Udostępnij za pośrednictwem


Dictionary<TKey, TValue>.IDictionary.Item Property

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

Gets or sets the value with the specified key.

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

Syntax

'Declaration
Private Property Item ( _
    key As Object _
) As Object Implements IDictionary.Item
Object IDictionary.this[
    Object key
] { get; set; }

Parameters

Property Value

Type: System.Object
The value associated with the specified key, or nulla null reference (Nothing in Visual Basic) if key is not in the dictionary or key is of a type that is not assignable to the key type TKey of the Dictionary<TKey, TValue>.

Implements

IDictionary.Item[Object]

Exceptions

Exception Condition
ArgumentNullException

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

ArgumentException

A value is being assigned, and key is of a type that is not assignable to the key type TKey of the Dictionary<TKey, TValue>.

-or-

A value is being assigned, and value is of a type that is not assignable to the value type TValue of the Dictionary<TKey, TValue>.

Remarks

This property provides the ability to access a specific value in the collection by using the following C# 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. 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.

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

Getting or setting the value of this property approaches an O(1) operation.

Examples

The following code example shows how to use the IDictionary.Item property (the indexer in C#) of the System.Collections.IDictionary interface with a Dictionary<TKey, TValue>, and ways the property differs from the Dictionary<TKey, TValue>.Item property.

The example shows that, like the Dictionary<TKey, TValue>.Item property, the Dictionary<TKey, TValue>.IDictionary.Item property can change the value associated with an existing key and can be used to add a new key/value pair if the specified key is not in the dictionary. The example also shows that unlike the Dictionary<TKey, TValue>.Item property, the Dictionary<TKey, TValue>.IDictionary.Item property does not throw an exception if key is not in the dictionary, returning a null reference instead. Finally, the example demonstrates that getting the Dictionary<TKey, TValue>.IDictionary.Item property returns a null reference if key is not the correct data type, and that setting the property throws an exception if key is not the correct data type.

The code example is part of a larger example, including output, provided for the IDictionary.Add method.

Imports System.Collections
Imports System.Collections.Generic

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' Create a new dictionary of strings, with string keys,
      ' and access it using the IDictionary interface.
      '
      Dim openWith As IDictionary = _
          New Dictionary(Of String, String)

      ' Add some elements to the dictionary. There are no 
      ' duplicate keys, but some of the values are duplicates.
      ' IDictionary.Add throws an exception if incorrect types
      ' are supplied for key or value.
      openWith.Add("txt", "notepad.exe")
      openWith.Add("bmp", "paint.exe")
      openWith.Add("dib", "paint.exe")
      openWith.Add("rtf", "wordpad.exe")


...


' 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 returns Nothing if the key
' is of the wrong data type.
outputBlock.Text &= String.Format("The default Item property returns Nothing" _
    & " if the key is of the wrong type:") & vbCrLf
outputBlock.Text &= String.Format("For key = 2, value = {0}.", _
    openWith(2)) & vbCrLf

' The default Item property throws an exception when setting
' a value if the key is of the wrong data type.
Try
   openWith(2) = "This does not get added."
Catch
   outputBlock.Text &= String.Format("A key of the wrong type was specified" _
       & " when setting the default Item property.") & vbCrLf
End Try


...


' Unlike the default Item property on the Dictionary class
' itself, IDictionary.Item does not throw an exception
' if the requested key is not in the dictionary.
outputBlock.Text &= String.Format("For key = ""tif"", value = {0}.", _
    openWith("tif")) & vbCrLf


...



   End Sub

End Class
using System;
using System.Collections;
using System.Collections.Generic;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Create a new dictionary of strings, with string keys,
      // and access it using the IDictionary interface.
      //
      IDictionary openWith = new Dictionary<string, string>();

      // Add some elements to the dictionary. There are no 
      // duplicate keys, but some of the values are duplicates.
      // IDictionary.Add throws an exception if incorrect types
      // are supplied for key or value.
      openWith.Add("txt", "notepad.exe");
      openWith.Add("bmp", "paint.exe");
      openWith.Add("dib", "paint.exe");
      openWith.Add("rtf", "wordpad.exe");


...


// 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 returns null if the key is of the wrong data 
// type.
outputBlock.Text += String.Format("The indexer returns null"
    + " if the key is of the wrong type:") + "\n";
outputBlock.Text += String.Format("For key = 2, value = {0}.",
    openWith[2]) + "\n";

// The indexer throws an exception when setting a value
// if the key is of the wrong data type.
try
{
   openWith[2] = "This does not get added.";
}
catch (ArgumentException)
{
   outputBlock.Text += String.Format("A key of the wrong type was specified"
       + " when assigning to the indexer.") + "\n";
}


...


// Unlike the default Item property on the Dictionary class
// itself, IDictionary.Item does not throw an exception
// if the requested key is not in the dictionary.
outputBlock.Text += String.Format("For key = \"tif\", value = {0}.",
    openWith["tif"]) + "\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.