public class Dictionary<TKey,TValue>: IDictionary<TKey,TValue>, ICollection<KeyValuePair<TKey,TValue>>, IEnumerable<KeyValuePair<TKey,TValue>>, IDictionary, ICollection, IEnumerable
Object
Dictionary<TKey,TValue>This type implements ICollection, IDictionary, IEnumerable, System.Collections.Generic.ICollection<KeyValuePair<TKey,TValue>>, IDictionary<TKey,TValue>, and System.Collections.Generic.IEnumerable<KeyValuePair<TKey,TValue>>.
mscorlib
BCL
Represents a collection of key/value pairs that are organized based on the key.
Each element is a key/value pair that can be retrieved as a System.Collections.Generic.KeyValuePair<TKey,TValue> object.Dictionary<TKey,TValue> requires an equality comparer implementation to perform comparisons. If no equality comparer is provided, the following default equality comparer approach is used: If type
TKey
implements System.IEquatable<TKey>, that implementation is used; otherwise,TKey
's implementations of System.Object.Equals(Object) and System.Object.GetHashCode() are used. In any case, you can specify a System.Collections.Generic.IEqualityComparer<TKey> implementation in a constructor overload that accepts an equality comparer parameter.After its insertion in a dictionary, changes to the value of a key that affect the equality comparer render the dictionary's behavior unspecified. Every key in a dictionary must be unique according to the equality comparer. A key cannot be
null
, but a value can be, if the value typeTValue
is a reference type.The capacity of a dictionary is the number of elements that dictionary can hold. As elements are added to a dictionary, the capacity is automatically increased.
This type contains a member that is a nested type, called
Enumerator
. AlthoughEnumerator
is a member of this type,Enumerator
is not described here; instead, it is described in its own entry,Dictionary<TKey,TValue>.Enumerator
.
System.Collections.Generic Namespace
Dictionary<TKey,TValue> Constructors
Dictionary<TKey,TValue>() Constructor
Dictionary<TKey,TValue>(System.Collections.Generic.IEqualityComparer<TKey>) Constructor
Dictionary<TKey,TValue>(System.Collections.Generic.IDictionary<TKey,TValue>) Constructor
Dictionary<TKey,TValue>(int) Constructor
Dictionary<TKey,TValue>(System.Collections.Generic.IDictionary<TKey,TValue>, System.Collections.Generic.IEqualityComparer<TKey>) Constructor
Dictionary<TKey,TValue>(int, System.Collections.Generic.IEqualityComparer<TKey>) Constructor
Dictionary<TKey,TValue> Methods
Dictionary<TKey,TValue>.Add Method
Dictionary<TKey,TValue>.Clear Method
Dictionary<TKey,TValue>.ContainsKey Method
Dictionary<TKey,TValue>.ContainsValue Method
Dictionary<TKey,TValue>.GetEnumerator Method
Dictionary<TKey,TValue>.Remove Method
Dictionary<TKey,TValue>.System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>.Add Method
Dictionary<TKey,TValue>.System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>.Contains Method
Dictionary<TKey,TValue>.System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>.CopyTo Method
Dictionary<TKey,TValue>.System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>.Remove Method
Dictionary<TKey,TValue>.System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>>.GetEnumerator Method
Dictionary<TKey,TValue>.System.Collections.ICollection.CopyTo Method
Dictionary<TKey,TValue>.System.Collections.IDictionary.Add Method
Dictionary<TKey,TValue>.System.Collections.IDictionary.Contains Method
Dictionary<TKey,TValue>.System.Collections.IDictionary.GetEnumerator Method
Dictionary<TKey,TValue>.System.Collections.IDictionary.Remove Method
Dictionary<TKey,TValue>.System.Collections.IEnumerable.GetEnumerator Method
Dictionary<TKey,TValue>.TryGetValue Method
Dictionary<TKey,TValue> Properties
Dictionary<TKey,TValue>.Count Property
Dictionary<TKey,TValue>.Item Property
Dictionary<TKey,TValue>.Keys Property
Dictionary<TKey,TValue>.System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>.IsReadOnly Property
Dictionary<TKey,TValue>.System.Collections.Generic.IDictionary<TKey,TValue>.Keys Property
Dictionary<TKey,TValue>.System.Collections.Generic.IDictionary<TKey,TValue>.Values Property
Dictionary<TKey,TValue>.System.Collections.ICollection.IsSynchronized Property
Dictionary<TKey,TValue>.System.Collections.ICollection.SyncRoot Property
Dictionary<TKey,TValue>.System.Collections.IDictionary.IsFixedSize Property
Dictionary<TKey,TValue>.System.Collections.IDictionary.IsReadOnly Property
Dictionary<TKey,TValue>.System.Collections.IDictionary.Item Property
Dictionary<TKey,TValue>.System.Collections.IDictionary.Keys Property
Dictionary<TKey,TValue>.System.Collections.IDictionary.Values Property
Dictionary<TKey,TValue>.Values Property
public Dictionary()
Initializes a new dictionary that is empty, has the default initial capacity, and uses the default equality comparer.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
public Dictionary(IEqualityComparer<TKey> comparer)
Initializes a new dictionary that is empty, has the default initial capacity, and uses the specified equality comparer.
- comparer
- The equality comparer implementation to use when comparing keys.
-or-
null
to use the default equality comparer for the type of the key.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
public Dictionary(IDictionary<TKey,TValue> dictionary)
Initializes a new dictionary that contains elements copied from the specified dictionary, has sufficient capacity to accommodate the number of elements copied, and uses the default equality comparer.
- dictionary
- The dictionary whose elements are to be copied to the new dictionary.
Exception Type Condition ArgumentException dictionary contains one or more duplicate keys. ArgumentNullException dictionary is null
.
Every key in a dictionary must be unique according to the default equality comparer; otherwise, a ArgumentException is thrown; likewise, every key in the source dictionary must also be unique according to the default equality comparer.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
public Dictionary(int capacity)
Initializes a new dictionary that is empty, has the specified initial capacity, and uses the default equality comparer.
- capacity
- The initial number of elements that the dictionary can contain.
Exception Type Condition ArgumentOutOfRangeException capacity is less than zero.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
public Dictionary(IDictionary<TKey,TValue> dictionary, IEqualityComparer<TKey> comparer)
Initializes a new dictionary that contains elements copied from the specified dictionary, has sufficient capacity to accommodate the number of elements copied, and uses the specified equality comparer.
- dictionary
- The dictionary whose elements are to be copied to the new dictionary.
- comparer
- The equality comparer implementation to use when comparing keys.
-or-
null
to use the default equality comparer for the type of the key.
Exception Type Condition ArgumentException dictionary contains one or more duplicate keys. ArgumentNullException dictionary is null
.
Every key in a dictionary must be unique according to the specified; otherwise, a ArgumentException is thrown; likewise, every key in the source dictionary must also be unique according to the specified equality comparer.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
public Dictionary(int capacity, IEqualityComparer<TKey> comparer)
Initializes a new dictionary that is empty, has the specified initial capacity, and uses the specified equality comparer.
- capacity
- The initial number of elements that the dictionary can contain.
- comparer
- The equality comparer implementation to use when comparing keys.
-or-
null
to use the default equality comparer for the type of the key.
Exception Type Condition ArgumentOutOfRangeException capacity is less than zero.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
public void Add(TKey key, TValue value)
Adds an element with the specified key and value to the dictionary.
- key
- The key of the element to add to the dictionary.
- value
- The value of the element to add to the dictionary.
Exception Type Condition ArgumentException An element with the same key already exists in the dictionary. ArgumentNullException key is null
.
You can also use the System.Collections.Generic.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 System.Collections.Generic.Dictionary<TKey,TValue>.Item(TKey)
property overwrites the old value. In contrast, the System.Collections.Generic.Dictionary<TKey,TValue>.Add(TKey,TValue)
method does not modify existing elements.If System.Collections.Generic.Dictionary<TKey,TValue>.Count already equals the capacity, the capacity of the dictionary is increased.
A key cannot be null, but a value can be, if the value type TValue is a reference type.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
public void Clear()
Removes all elements from the dictionary.
[Note: This method is implemented to support the IDictionary interface.]
System.Collections.Generic.ICollection<TKey>.Count gets set to zero, and references to other objects from elements of the collection are also released. The capacity remains unchanged.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
public virtual bool ContainsKey(TKey key)
Determines whether the dictionary contains an element with a specific key.
- key
- The key to locate in the dictionary.
true
, if an element whose key is key is found in the dictionary; otherwise,false
.
Exception Type Condition ArgumentNullException key is null
.
This implementation is close to O(1) in most cases.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
public bool ContainsValue(TValue value)
Determines whether the dictionary contains an element with a specific value.
- value
- The value to locate in the dictionary.
true
, if an element whose value is value is found in the dictionary; otherwise,false
.
This method determines equality using the default equality comparer for the value type TValue. If TValue implements System.IEquatable<TValue>, that type is used. Otherwise, System.Object.Equals is used.This method performs a linear search; therefore, the average execution time is proportional to System.Collections.Generic.Dictionary<TKey,TValue>.Count. That is, this method is an O(n) operation, where n is System.Collections.Generic.Dictionary<TKey,TValue>.Count.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
public Dictionary<TKey,TValue>.Enumerator GetEnumerator()
Returns an enumerator that can be used to iterate over the dictionary.
An enumerator for the dictionary.
[Usage: For a detailed description regarding the use of an enumerator, see System.Collections.Generic.IEnumerator<TKey>.]
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
public bool Remove(TKey key)
Removes the element with the specified key from the dictionary.
- key
- The key of the element to be removed from the dictionary.
true
if the element containing key is successfully removed; otherwise,false
. This method also returnsfalse
if key was not found in the dictionary.
Exception Type Condition ArgumentNullException key is null
.
If the dictionary does not contain an element with the specified key, the dictionary remains unchanged. No exception is thrown.This method shall be implemented with efficiency that is at least an O(1) operation.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
void ICollection<KeyValuePair<TKey,TValue>>.Add(KeyValuePair<TKey,TValue> keyValuePair)
This method is implemented to support the System.Collections.Generics.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>> interface.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
bool ICollection<KeyValuePair<TKey,TValue>>.Contains(KeyValuePair<TKey,TValue> keyValuePair)
This method is implemented to support the System.Collections.Generics.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>> interface.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
void ICollection<KeyValuePair<TKey,TValue>>.CopyTo(KeyValuePair<TKey,TValue>[] array, int index)
This method is implemented to support the System.Collections.Generics.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>> interface.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
bool ICollection<KeyValuePair<TKey,TValue>>.Remove(KeyValuePair<TKey,TValue> keyValuePair)
This method is implemented to support the System.Collections.Generics.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>> interface.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
IEnumerator<T> IEnumerable<KeyValuePair<TKey,TValue>>.GetEnumerator()
This method is implemented to support the System.Collections.Generics.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>> interface.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
void ICollection.CopyTo(Array array, int index)
This method is implemented to support the ICollection interface.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
void IDictionary.Add(object key, object value)
This method is implemented to support the IDictionary interface.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
bool IDictionary.Contains(object key)
This method is implemented to support the IDictionary interface.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
IDictionaryEnumerator IDictionary.GetEnumerator()
This method is implemented to support the IDictionary interface.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
void IDictionary.Remove(object key)
This method is implemented to support the IDictionary interface.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
IEnumerator IEnumerable.GetEnumerator()
This method is implemented to support the IEnumerable interface.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
public bool TryGetValue(TKey key, out TValue value)
Gets the value associated with the specified key.
- key
- The key of the element to locate in the dictionary.
- value
- When this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of this parameter.
true
if the dictionary contains an element with the specified key; otherwise,false
.
Exception Type Condition ArgumentNullException key is null
.
This method combines the functionality of the System.Collections.Generic.Dictionary<TKey,TValue>.ContainsKey method and the System.Collections.Generic.Dictionary<TKey,TValue>.Item property.The default value for value types is zero while that for reference types is
null
.This method is an O(1) operation.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
public int Count { get; }
Gets the number of key/value pairs contained in the dictionary.
The number of key/value pairs contained in the dictionary.
This property is read-only.Retrieving the value of this property is an O(1) operation.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
public TValue this[TKey key] { get; set; }
Gets or sets the value associated with the specified key.
- 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.
Exception Type Condition 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 null.You can also use the System.Collections.Generic.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 System.Collections.Generic.Dictionary<TKey,TValue>.Item(TKey)
property overwrites the old value. In contrast, the System.Collections.Generic.Dictionary<TKey,TValue>.Add(TKey,TValue)
method does not modify existing elements.A key cannot be null, 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.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
public KeyCollection<TKey,TValue> Keys { get; }
Gets a collection that contains the keys in the dictionary.
A collection of the keys in the dictionary.
This property is read-only.The order of the keys in the key collection is unspecified, but it is the same order as the associated values in the value collection returned by the System.Collections.Generic.Dictionary<TKey,TValue>.Values property.
If the dictionary is modified, or the value of any key in the dictionary is modified, the behavior of the key collection is unspecified.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
bool ICollection<KeyValuePair<TKey,TValue>>.IsReadOnly { get; }
This read-only property is implemented to support the System.Collections.Generics.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>> interface.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
ICollection<TKey> IDictionary<TKey,TValue>.Keys { get; }
This read-only property is implemented to support the IDictionary<TKey,TValue> interface.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
ICollection<TValue> IDictionary<TKey,TValue>.Values { get; }
This read-only property is implemented to support the IDictionary<TKey,TValue> interface.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
bool ICollection.IsSynchronized { get; }
This read-only property is implemented to support the ICollection interface.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
object ICollection.SyncRoot { get; }
This read-only property is implemented to support the ICollection interface.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
bool IDictionary.IsFixedSize { get; }
This read-only property is implemented to support the IDictionary interface.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
bool IDictionary.IsReadOnly { get; }
This read-only property is implemented to support the IDictionary interface.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
object IDictionary.this[object key] { get; set; }
This read-only property is implemented to support the IDictionary interface.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
ICollection IDictionary.Keys { get; }
This read-only property is implemented to support the IDictionary interface.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
ICollection IDictionary.Values { get; }
This read-only property is implemented to support the IDictionary interface.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace
public ValueCollection<TKey,TValue> Values { get; }
Gets a collection that contains the values in the dictionary.
A collection of the values in the dictionary.
This property is read-only.The order of the values in the value collection is unspecified, but it is the same order as the associated values in the key collection returned by the System.Collections.Generic.Dictionary<TKey,TValue>.Keys property.
The returned value collection is not a static copy; instead, it refers back to the values in the original dictionary. Therefore, changes to the dictionary continue to be reflected in the value collection.
System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace