System.Collections.Generic.Dictionary<TKey,TValue> Class

public class Dictionary<TKey,TValue>: IDictionary<TKey,TValue>, ICollection<KeyValuePair<TKey,TValue>>, IEnumerable<KeyValuePair<TKey,TValue>>, IDictionary, ICollection, IEnumerable

Base Types

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>>.

Assembly

mscorlib

Library

BCL

Summary

Represents a collection of key/value pairs that are organized based on the key.

Description

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 type TValue 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 . Although Enumerator is a member of this type, Enumerator is not described here; instead, it is described in its own entry, Dictionary<TKey,TValue>.Enumerator .

See Also

System.Collections.Generic Namespace

Members

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


Dictionary<TKey,TValue>() Constructor

public Dictionary()

Summary

Initializes a new dictionary that is empty, has the default initial capacity, and uses the default equality comparer.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>(System.Collections.Generic.IEqualityComparer<TKey>) Constructor

public Dictionary(IEqualityComparer<TKey> comparer)

Summary

Initializes a new dictionary that is empty, has the default initial capacity, and uses the specified equality comparer.

Parameters

comparer
The equality comparer implementation to use when comparing keys.

-or-

null to use the default equality comparer for the type of the key.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>(System.Collections.Generic.IDictionary<TKey,TValue>) Constructor

public Dictionary(IDictionary<TKey,TValue> dictionary)

Summary

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.

Parameters

dictionary
The dictionary whose elements are to be copied to the new dictionary.

Exceptions

Exception TypeCondition
ArgumentExceptiondictionary contains one or more duplicate keys.
ArgumentNullExceptiondictionary is null .

Description

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.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>(int) Constructor

public Dictionary(int capacity)

Summary

Initializes a new dictionary that is empty, has the specified initial capacity, and uses the default equality comparer.

Parameters

capacity
The initial number of elements that the dictionary can contain.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptioncapacity is less than zero.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>(System.Collections.Generic.IDictionary<TKey,TValue>, System.Collections.Generic.IEqualityComparer<TKey>) Constructor

public Dictionary(IDictionary<TKey,TValue> dictionary, IEqualityComparer<TKey> comparer)

Summary

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.

Parameters

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.

Exceptions

Exception TypeCondition
ArgumentExceptiondictionary contains one or more duplicate keys.
ArgumentNullExceptiondictionary is null .

Description

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.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>(int, System.Collections.Generic.IEqualityComparer<TKey>) Constructor

public Dictionary(int capacity, IEqualityComparer<TKey> comparer)

Summary

Initializes a new dictionary that is empty, has the specified initial capacity, and uses the specified equality comparer.

Parameters

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.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptioncapacity is less than zero.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.Add Method

public void Add(TKey key, TValue value)

Summary

Adds an element with the specified key and value to the dictionary.

Parameters

key
The key of the element to add to the dictionary.
value
The value of the element to add to the dictionary.

Exceptions

Exception TypeCondition
ArgumentExceptionAn element with the same key already exists in the dictionary.
ArgumentNullExceptionkey is null .

Description

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.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.Clear Method

public void Clear()

Summary

Removes all elements from the dictionary.

Description

[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.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.ContainsKey Method

public virtual bool ContainsKey(TKey key)

Summary

Determines whether the dictionary contains an element with a specific key.

Parameters

key
The key to locate in the dictionary.

Return Value

true , if an element whose key is key is found in the dictionary; otherwise, false .

Exceptions

Exception TypeCondition
ArgumentNullExceptionkey is null .

Description

This implementation is close to O(1) in most cases.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.ContainsValue Method

public bool ContainsValue(TValue value)

Summary

Determines whether the dictionary contains an element with a specific value.

Parameters

value
The value to locate in the dictionary.

Return Value

true , if an element whose value is value is found in the dictionary; otherwise, false .

Description

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.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.GetEnumerator Method

public Dictionary<TKey,TValue>.Enumerator GetEnumerator()

Summary

Returns an enumerator that can be used to iterate over the dictionary.

Return Value

An enumerator for the dictionary.

Description

[Usage: For a detailed description regarding the use of an enumerator, see System.Collections.Generic.IEnumerator<TKey>.]

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.Remove Method

public bool Remove(TKey key)

Summary

Removes the element with the specified key from the dictionary.

Parameters

key
The key of the element to be removed from the dictionary.

Return Value

true if the element containing key is successfully removed; otherwise, false . This method also returns false if key was not found in the dictionary.

Exceptions

Exception TypeCondition
ArgumentNullExceptionkey is null .

Description

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.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>.Add Method

void ICollection<KeyValuePair<TKey,TValue>>.Add(KeyValuePair<TKey,TValue> keyValuePair)

Summary

This method is implemented to support the System.Collections.Generics.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>> interface.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>.Contains Method

bool ICollection<KeyValuePair<TKey,TValue>>.Contains(KeyValuePair<TKey,TValue> keyValuePair)

Summary

This method is implemented to support the System.Collections.Generics.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>> interface.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>.CopyTo Method

void ICollection<KeyValuePair<TKey,TValue>>.CopyTo(KeyValuePair<TKey,TValue>[] array, int index)

Summary

This method is implemented to support the System.Collections.Generics.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>> interface.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>.Remove Method

bool ICollection<KeyValuePair<TKey,TValue>>.Remove(KeyValuePair<TKey,TValue> keyValuePair)

Summary

This method is implemented to support the System.Collections.Generics.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>> interface.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>>.GetEnumerator Method

IEnumerator<T> IEnumerable<KeyValuePair<TKey,TValue>>.GetEnumerator()

Summary

This method is implemented to support the System.Collections.Generics.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>> interface.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.System.Collections.ICollection.CopyTo Method

void ICollection.CopyTo(Array array, int index)

Summary

This method is implemented to support the ICollection interface.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.System.Collections.IDictionary.Add Method

void IDictionary.Add(object key, object value)

Summary

This method is implemented to support the IDictionary interface.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.System.Collections.IDictionary.Contains Method

bool IDictionary.Contains(object key)

Summary

This method is implemented to support the IDictionary interface.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.System.Collections.IDictionary.GetEnumerator Method

IDictionaryEnumerator IDictionary.GetEnumerator()

Summary

This method is implemented to support the IDictionary interface.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.System.Collections.IDictionary.Remove Method

void IDictionary.Remove(object key)

Summary

This method is implemented to support the IDictionary interface.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.System.Collections.IEnumerable.GetEnumerator Method

IEnumerator IEnumerable.GetEnumerator()

Summary

This method is implemented to support the IEnumerable interface.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.TryGetValue Method

public bool TryGetValue(TKey key, out TValue value)

Summary

Gets the value associated with the specified key.

Parameters

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.

Return Value

true if the dictionary contains an element with the specified key; otherwise, false .

Exceptions

Exception TypeCondition
ArgumentNullExceptionkey is null .

Description

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.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.Count Property

public int Count { get; }

Summary

Gets the number of key/value pairs contained in the dictionary.

Property Value

The number of key/value pairs contained in the dictionary.

Description

This property is read-only.

Retrieving the value of this property is an O(1) operation.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.Item Property

public TValue this[TKey key] { get; set; }

Summary

Gets or sets the value associated with the specified key.

Parameters

key
The key whose value is to be gotten or set.

Property Value

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.

Exceptions

Exception TypeCondition
ArgumentNullExceptionkey is null .

KeyNotFoundExceptionDuring a get attempt, key is not found in the dictionary.

Description

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.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.Keys Property

public KeyCollection<TKey,TValue> Keys { get; }

Summary

Gets a collection that contains the keys in the dictionary.

Property Value

A collection of the keys in the dictionary.

Description

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.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>.IsReadOnly Property

bool ICollection<KeyValuePair<TKey,TValue>>.IsReadOnly { get; }

Summary

This read-only property is implemented to support the System.Collections.Generics.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>> interface.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.System.Collections.Generic.IDictionary<TKey,TValue>.Keys Property

ICollection<TKey> IDictionary<TKey,TValue>.Keys { get; }

Summary

This read-only property is implemented to support the IDictionary<TKey,TValue> interface.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.System.Collections.Generic.IDictionary<TKey,TValue>.Values Property

ICollection<TValue> IDictionary<TKey,TValue>.Values { get; }

Summary

This read-only property is implemented to support the IDictionary<TKey,TValue> interface.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.System.Collections.ICollection.IsSynchronized Property

bool ICollection.IsSynchronized { get; }

Summary

This read-only property is implemented to support the ICollection interface.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.System.Collections.ICollection.SyncRoot Property

object ICollection.SyncRoot { get; }

Summary

This read-only property is implemented to support the ICollection interface.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.System.Collections.IDictionary.IsFixedSize Property

bool IDictionary.IsFixedSize { get; }

Summary

This read-only property is implemented to support the IDictionary interface.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.System.Collections.IDictionary.IsReadOnly Property

bool IDictionary.IsReadOnly { get; }

Summary

This read-only property is implemented to support the IDictionary interface.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.System.Collections.IDictionary.Item Property

object IDictionary.this[object key] { get; set; }

Summary

This read-only property is implemented to support the IDictionary interface.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.System.Collections.IDictionary.Keys Property

ICollection IDictionary.Keys { get; }

Summary

This read-only property is implemented to support the IDictionary interface.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.System.Collections.IDictionary.Values Property

ICollection IDictionary.Values { get; }

Summary

This read-only property is implemented to support the IDictionary interface.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace

Dictionary<TKey,TValue>.Values Property

public ValueCollection<TKey,TValue> Values { get; }

Summary

Gets a collection that contains the values in the dictionary.

Property Value

A collection of the values in the dictionary.

Description

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.

See Also

System.Collections.Generic.Dictionary<TKey,TValue> Class, System.Collections.Generic Namespace