← Interactive API Explorer
Namespace: System.Collections.Generic

Dictionary<TKey, TValue>

Represents a collection of keys and values.

Properties (3)

int Count
Gets the number of key/value pairs contained in the Dictionary.
Value: The number of key/value pairs contained in the Dictionary.
KeyCollection<TKey, TValue> Keys
Gets a collection containing the keys in the Dictionary.
Value: A KeyCollection containing the keys in the Dictionary.
TValue Item(T)
Gets or sets the value associated with the specified key.
Value: The value associated with the specified key. If the specified key is not found, a get operation throws a KeyNotFoundException, and a set operation creates a new element with the specified key.

Methods (9)

Dictionary<TKey, TValue>(Int32)Constructor
Initializes a new instance of the Dictionary class that is empty, has the specified initial capacity, and uses the default equality comparer for the key type.
ParameterDescription
capacityThe initial number of elements that the Dictionary can contain.
Dictionary<TKey, TValue>(Int32,IEqualityComparer<T>)Constructor
Initializes a new instance of the Dictionary class that is empty, has the specified initial capacity, and uses the specified IEqualityComparer.
ParameterDescription
capacityThe initial number of elements that the Dictionary can contain.
comparerThe IEqualityComparer implementation to use when comparing keys, or null to use the default EqualityComparer for the type of the key.
Dictionary<TKey, TValue>(IEqualityComparer<T>)Constructor
Initializes a new instance of the Dictionary class that is empty, has the default initial capacity, and uses the specified IEqualityComparer.
ParameterDescription
comparerThe IEqualityComparer implementation to use when comparing keys, or null to use the default EqualityComparer for the type of the key.
void Add(T,TValue)
Adds the specified key and value to the dictionary.
ParameterDescription
keyThe key of the element to add.
valueThe value of the element to add. The value can be null for reference types.
bool TryGetValue(T,TValue@)
Gets the value associated with the specified key.
ParameterDescription
keyThe key of the value to get.
valueWhen this method returns, contains the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized.
Returns: true if the Dictionary contains an element with the specified key; otherwise, false.
bool ContainsKey(T)
Determines whether the Dictionary contains the specified key.
ParameterDescription
keyThe key to locate in the Dictionary.
Returns: true if the Dictionary contains an element with the specified key; otherwise, false.
bool Remove(T)
Removes the value with the specified key from the Dictionary.
ParameterDescription
keyThe key of the element to remove.
Returns: true if the element is successfully found and removed; otherwise, false. This method returns false if key is not found in the Dictionary.
void Clear
Removes all keys and values from the Dictionary.
Enumerator<TKey, TValue> GetEnumerator
Returns an enumerator that iterates through the Dictionary.
Returns: A Enumerator structure for the Dictionary.