This documentation is archived and is not being maintained.

IDictionary.Item Property

Gets or sets the element with the specified key.

Namespace: System.Collections.Generic
Assembly: mscorlib (in mscorlib.dll)

TValue this [
	TKey key
] { get; set; }
/** @property */
TValue get_Item (TKey key)

/** @property */
void set_Item (TKey key, TValue value)

Not applicable.



The key of the element to get or set.

Property Value

The element with the specified key.

Exception typeCondition


key is a null reference (Nothing in Visual Basic).


The property is retrieved and key is not found.


The property is set and the IDictionary is read-only.

This property provides the ability to access a specific element in the collection by using the following syntax: myCollection[key] (myCollection(key) in Visual Basic).

You can also use the Item property to add new elements by setting the value of a key that does not exist in the dictionary; for example, myCollection["myNonexistentKey"] = myValue in C# (myCollection("myNonexistentKey") = myValue in Visual Basic). However, if the specified key already exists in the dictionary, setting the Item property overwrites the old value. In contrast, the Add method does not modify existing elements.

Implementations can vary in how they determine equality of objects; for example, the List class uses Comparer.Default, whereas the Dictionary class allows the user to specify the IComparer implementation to use for comparing keys.

The C# language uses the this keyword to define the indexers instead of implementing the Item property. Visual Basic implements Item as a default property, which provides the same indexing functionality.

Implementations can vary in whether they allow key to be a null reference (Nothing in Visual Basic).

The following code example uses the Item property (the indexer in C#) to retrieve values, demonstrating that a KeyNotFoundException is thrown when a requested key is not present, and showing that the value associated with a key can be replaced.

The example also shows how to use the TryGetValue method as a more efficient way to retrieve values if a program often must try key values that are not in the dictionary.

This code is part of a larger example that can be compiled and executed. See System.Collections.Generic.IDictionary.

    // The Item property is another name for the indexer, so you 
    // can omit its name when accessing elements. 
    Console.WriteLine("For key = \"rtf\", value = {0}.", 

    // The indexer can be used to change the value associated
    // with a key.
    openWith["rtf"] = "winword.exe";
    Console.WriteLine("For key = \"rtf\", value = {0}.", 

    // If a key does not exist, setting the indexer for that key
    // adds a new key/value pair.
    openWith["doc"] = "winword.exe";


    // The indexer throws an exception if the requested key is
    // not in the dictionary.
        Console.WriteLine("For key = \"tif\", value = {0}.", 
    catch (KeyNotFoundException)
        Console.WriteLine("Key = \"tif\" is not found.");


    // When a program often has to try keys that turn out not to
    // be in the dictionary, TryGetValue can be a more efficient 
    // way to retrieve values.
    string value = "";
    if (openWith.TryGetValue("tif", out value))
        Console.WriteLine("For key = \"tif\", value = {0}.", value);
        Console.WriteLine("Key = \"tif\" is not found.");

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0