KeyedCollection<'TKey, 'TItem>.Item Property ('TKey)

 

Gets the element with the specified key.

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

member Item : 
        key:'TKey -> 'TItem with get

Parameters

key
Type: TKey

The key of the element to get.

Property Value

Type: TItem

The element with the specified key. If an element with the specified key is not found, an exception is thrown.

Exception Condition
ArgumentNullException

key is null.

KeyNotFoundException

An element with the specified key does not exist in the collection.

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

System_CAPS_noteNote

This property is distinct from the inherited Collection<'T>.Item property, which gets and sets elements by numeric index. However, if TKey is of type Int32, this property masks the inherited property. In that case, you can access the inherited property by casting the KeyedCollection<'TKey, 'TItem> to its base type. For example, KeyedCollection<int, MyType> (KeyedCollection(Of Integer, MyType) in Visual Basic, KeyedCollection<int, MyType^> in C++) can be cast to Collection<MyType> (Collection(Of MyType) in Visual Basic, Collection<MyType^> in C++).

If the KeyedCollection<'TKey, 'TItem> has a lookup dictionary, key is used to retrieve the element from the dictionary. If there is no lookup dictionary, the key of each element is extracted using the GetKeyForItem method and compared with the specified key.

The C# language uses the 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.

Retrieving the value of this property is an O(1) operation if the KeyedCollection<'TKey, 'TItem> has a lookup dictionary; otherwise it is an O(n) operation, where n is Count.

This code example shows the minimum code necessary to derive a collection class from KeyedCollection<'TKey, 'TItem>: overriding the GetKeyForItem method and providing a public constructor that delegates to a base class constructor. The code example also demonstrates many of the properties and methods inherited from KeyedCollection<'TKey, 'TItem> and Collection<'T> classes.

The code example calls both the KeyedCollection<'TKey, 'TItem>.Item property, which is read-only and retrieves by key, and the Collection<'T>.Item property, which is settable and retrieves by index. It shows how to access the latter property when the objects in the derived collection have integer keys, indistinguishable from the integers used for indexed retrieval.

The SimpleOrder class is a very simple requisition list that contains OrderItem objects, each of which represents a line item in the order. The key of OrderItem is immutable, an important consideration for classes that derive from KeyedCollection<'TKey, 'TItem>. For a code example that uses mutable keys, see ChangeItemKey.

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
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: