SortedDictionary<'TKey, 'TValue>.IDictionary.Item Property (Object)

 

Gets or sets the element with the specified key.

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

private abstract Item : 
        key:Object -> Object with get, set
private override Item : 
        key:Object -> Object with get, set

Parameters

key
Type: System.Object

The key of the element to get.

Property Value

Type: System.Object

The element with the specified key, or null if key is not in the dictionary or key is of a type that is not assignable to the key type TKey of the SortedDictionary<'TKey, 'TValue>.

Exception Condition
ArgumentNullException

key is null.

ArgumentException

A value is being assigned, and key is of a type that is not assignable to the key type TKey of the SortedDictionary<'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 SortedDictionary<'TKey, 'TValue>.

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 Itemproperty 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 IDictionary.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 the value of this property is an O(log n) operation; setting the property is also an O(log n) 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 SortedDictionary<'TKey, 'TValue>, and ways the property differs from the SortedDictionary<'TKey, 'TValue>.Item property.

The example shows that, like the SortedDictionary<'TKey, 'TValue>.Item property, the SortedDictionary<'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 SortedDictionary<'TKey, 'TValue>.Item property, the SortedDictionary<'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 SortedDictionary<'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.
No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
Show: