Show / Hide Table of Contents

TypeMap Data Type

This proposal offers a data type that can be used to map the values to the generic argument. It's very similar to classic dictionary but the keys are generic types:

public void Add<TKey>(TValue value);
public bool TryGetValue<TKey>(out TValue value);

This is useful when you want to build an association between the type and some value. At first look, the feature can be implemented using Dictionary<Type, TValue> where the keys are represented by the reflected types using typeof operator. But in practice, the dictionary demonstrates lower performance.

As a response to the proposal, .NEXT provides the following features:

  • ITypeMap<TValue> interface that provides a set of methods similar to IDictionary<TKey,TValue> interface
  • TypeMap<TValue> is an implementation of ITypeMap<TValue> interface with very fast access to the values associated with generic arguments. However, this type is not thread-safe
  • ConcurrentTypeMap<TValue> is a thread-safe implementation of ITypeMap<TValue> interface with a convenient methods for concurrent access

Both implementations are highly optimized for fast access to the value associated with the types.

Additionally, the library provides set-like counterpart of this API:

  • Non-generic ITypeMap interface that provides a set of methods similar to ISet<T>
  • Non-generic TypeMap is an implementation of ITypeMap interface with very fast acces to the typed values
  • Non-generic ConcurrentTypeMap is a thread-safe implementation of ITypeMap interface with a convenient methods for concurrent access
  • Edit this page
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾