KeyedCollection<'TKey, 'TItem>.GetKeyForItem Method ('TItem)

 

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

Parameters

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

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.

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: