Export (0) Print
Expand All

Dictionary(Of TKey, TValue).Item Property

Gets or sets the value associated with the specified key.

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

Public Property Item ( _
	key As TKey _
) As TValue


Type: TKey

The key of the value to get or set.

Property Value

Type: TValue
The value associated with the specified key. If the specified key is not found, a get operation throws a KeyNotFoundException, and a set operation creates a new element with the specified key.


IDictionary(Of TKey, TValue).Item
IReadOnlyDictionary(Of TKey, TValue).Item


key is Nothing.


The property is retrieved and key does not exist in the collection.

This property provides the ability to access a specific element 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(Of TKey, TValue). When you set the property value, if the key is in the Dictionary(Of TKey, TValue), the value associated with that key is replaced by the assigned value. If the key is not in the Dictionary(Of TKey, TValue), the key and value are added to the dictionary. In contrast, the Add method does not modify existing elements.

A key cannot be Nothing, but a value can be, if the value type TValue is a reference type.

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.

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

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 example is part of a larger example provided for the Dictionary(Of TKey, TValue) class. openWith is the name of the Dictionary used in this example.

' The Item property is the default property, so you  
' can omit its name when accessing elements. 
Console.WriteLine("For key = ""rtf"", value = {0}.", _

' The default Item property 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 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. 
    Console.WriteLine("For key = ""tif"", value = {0}.", _
    Console.WriteLine("Key = ""tif"" is not found.")
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
    Console.WriteLine("For key = ""tif"", value = {0}.", value)
    Console.WriteLine("Key = ""tif"" is not found.")
End If

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

© 2014 Microsoft