Interface IDictionary<TKey, TValue>

Namespace: System.Collections.Generic

Represents a collection of key/value pairs that are sorted according to the key. This interface is a generalization of a dictionary that maps keys to values.

The keys in a System.Collections.Generic.IDictionary<TKey, TValue> cannot be null references, although unmanaged types can be keys.

The values can be null references for reference types.

Syntax

public interface IDictionary<TKey, TValue> : ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, System.Collections.IEnumerable

Remarks

The IDictionary<TKey, TValue> interface represents a collection that contains keys and values.

For a list of the actual members of the IDictionary<TKey, TValue> interface, see Members.

The IDictionary<TKey, TValue> interface is the base interface for all generic dictionary classes.

The IDictionary<TKey, TValue> interface requires that the TKey type parameter be a reference type or a value type that supports equality comparison.

Members

Properties

IsFixedSize

bool IsFixedSize { get; }

Gets a value indicating whether the dictionary has a fixed size.

IsReadOnly

bool IsReadOnly { get; }

Gets a value indicating whether the dictionary is read-only.

Keys

ICollection<TKey> Keys { get; }

Gets an ICollection<TKey> that contains the keys of the dictionary.

Values

ICollection<TValue> Values { get; }

Gets an ICollection<TValue> that contains the values of the dictionary.

Item[TKey key]

TValue Item[TKey key] { get; set; }

Gets or sets the element with the specified key.

Methods

Add(TKey key, TValue value)

void Add(TKey key, TValue value)

Parameters

  • key: TKey
    The key of the element to add.
  • value: TValue
    The value of the element to add.

Exceptions

  • ArgumentNullException: key is null.
  • ArgumentException: An element with the same key already exists.

ContainsKey(TKey key)

bool ContainsKey(TKey key)

Parameters

  • key: TKey
    The key to locate in the dictionary.

Returns

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

Remove(TKey key)

bool Remove(TKey key)

Parameters

  • key: TKey
    The key of the element to remove.

Returns

true if the element is successfully removed; otherwise, false. If the dictionary does not contain an element with the specified key, the method does nothing.

TryGetValue(TKey key, out TValue value)

bool TryGetValue(TKey key, out TValue value)

Parameters

  • key: TKey
    The key of the element to get.
  • value: TValue (out)
    When this method returns, contains the value associated with the specified key, if the key is found; otherwise, it contains 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.

See Also

System.Collections.Generic Namespace

Dictionary<TKey, TValue> Class

ICollection<T> Interface