Export (0) Print
Expand All

KeyedCollection(TKey, TItem).GetKeyForItem Method

When implemented in a derived class, extracts the key from the specified element.

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

abstract GetKeyForItem : 
        item:'TItem -> 'TKey 


Type: TItem

The element from which to extract the key.

Return Value

Type: TKey
The key for the specified element.

If the key in the lookup dictionary is different from the key that is embedded in item, you cannot access item by using the key returned by GetKeyForItem.

You can implement this method to return a null reference (Nothing in Visual Basic) for a collection that contains items without keys, in which case the items can be accessed only by their index. This method is an O(1) operation.

Notes for Implementers

You must override this method to provide the dictionary with a way to extract keys from items in the dictionary.

This method is called internally. It is not necessary for it to be public.

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 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

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

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