Condividi tramite


IDictionary<TKey, TValue>.Item Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets or sets the element with the specified key.

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

Syntax

'Declaration
Property Item ( _
    key As TKey _
) As TValue
TValue this[
    TKey key
] { get; set; }

Parameters

  • key
    Type: TKey
    The key of the element to get or set.

Property Value

Type: TValue
The element with the specified key.

Exceptions

Exception Condition
ArgumentNullException

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

KeyNotFoundException

The property is retrieved and key is not found.

NotSupportedException

The property is set and the IDictionary<TKey, TValue> is read-only.

Remarks

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<T> class uses Comparer<T>.Default, whereas the Dictionary<TKey, TValue> class allows the user to specify the IComparer<T> 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 nulla null reference (Nothing in Visual Basic).

Examples

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<TKey, TValue>.

' The Item property is the default property, so you 
' can omit its name when accessing elements. 
outputBlock.Text &= String.Format("For key = ""rtf"", value = {0}.", _
    openWith("rtf")) & vbCrLf

' The default Item property can be used to change the value
' associated with a key.
openWith("rtf") = "winword.exe"
outputBlock.Text &= String.Format("For key = ""rtf"", value = {0}.", _
    openWith("rtf")) & vbCrLf

' If a key does not exist, setting the default item property
' for that key adds a new key/value pair.
openWith("doc") = "winword.exe"


...


' The default Item property throws an exception if the requested
' key is not in the dictionary.
Try
   outputBlock.Text &= String.Format("For key = ""tif"", value = {0}.", _
       openWith("tif")) & vbCrLf
Catch
   outputBlock.Text &= "Key = ""tif"" is not found." & vbCrLf
End Try


...


' 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.
Dim value As String = ""
If openWith.TryGetValue("tif", value) Then
   outputBlock.Text &= String.Format("For key = ""tif"", value = {0}.", value) & vbCrLf
Else
   outputBlock.Text &= "Key = ""tif"" is not found." & vbCrLf
End If
// The Item property is another name for the indexer, so you 
// can omit its name when accessing elements. 
outputBlock.Text += String.Format("For key = \"rtf\", value = {0}.",
    openWith["rtf"]) + "\n";

// The indexer can be used to change the value associated
// with a key.
openWith["rtf"] = "winword.exe";
outputBlock.Text += String.Format("For key = \"rtf\", value = {0}.",
    openWith["rtf"]) + "\n";

// 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.
try
{
   outputBlock.Text += String.Format("For key = \"tif\", value = {0}.",
       openWith["tif"]) + "\n";
}
catch (KeyNotFoundException)
{
   outputBlock.Text += "Key = \"tif\" is not found." + "\n";
}


...


// 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))
{
   outputBlock.Text += String.Format("For key = \"tif\", value = {0}.", value) + "\n";
}
else
{
   outputBlock.Text += "Key = \"tif\" is not found." + "\n";
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.