Gets or sets the value associated with the specified key.
This is the default property for this class.
public TValue this [TKey key] { set; get; }
- key
- The key whose value is to be gotten or set.
The value associated with the specified key. On a get attempt, if the specified key is not found, a KeyNotFoundException is thrown. On a set attempt, if the specified key is not found, a new element using the specified key is created.
Type Reason ArgumentNullException key is null. KeyNotFoundException During a get attempt, key is not found in the dictionary.
The default value for value types is zero while that for reference types is ..
You can also use the Dictionary<TKey, TValue>.Item(TKey) property to add new elements by setting the value of a key that does not exist in the dictionary. However, if the specified key already exists in the dictionary, setting the Dictionary<TKey, TValue>.Item(TKey) property overwrites the old value. In contrast, the Dictionary<TKey, TValue>.Add(TKey,TValue) method does not modify existing elements.
A key cannot be ., but a value can be, if the value type TValue is a reference type.
Retrieving the value of this property is an O(1) operation; setting the property is also an O(1) operation.
Namespace: System.Collections.Generic
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 2.0.0.0
Since: .NET 2.0