Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
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
Assemblies:   mscorlib (in mscorlib.dll)
  System.ObjectModel (in System.ObjectModel.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.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
© 2015 Microsoft