Export (0) Print
Expand All

Dictionary<TKey, TValue>::IDictionary::Item Property

Gets or sets the value with the specified key.

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

private:
virtual property Object^ Item[Object^ key] {
	Object^ get (Object^ key) sealed = IDictionary::Item::get;
	void set (Object^ key, Object^ value) sealed = IDictionary::Item::set;
}

Parameters

key
Type: System::Object

The key of the value to get.

Property Value

Type: System::Object
The value associated with the specified key, or nullptr 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

ExceptionCondition
ArgumentNullException

key is nullptr.

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>.

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.

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.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.5.3, 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.

Show:
© 2014 Microsoft