Item Property (TKey)
Collapse the table of content
Expand the table of content

KeyedCollection(Of TKey, TItem).Item Property (TKey)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Gets the element with the specified key.

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

'Declaration
Public ReadOnly Property Item ( _
	key As TKey _
) As TItem

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.

ExceptionCondition
ArgumentNullException

key is Nothing.

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

NoteNote:

This property is distinct from the inherited Collection(Of 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(Of 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(Of 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 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.

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

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft