System.Collections.ObjectModel.KeyedCollection<TKey,TItem> Class

Provides the abstract base class for a collection whose keys are embedded in the values.

See Also: KeyedCollection<TKey,TItem> Members


public abstract class KeyedCollection<TKey, TItem> : Collection<TItem>

Type Parameters

Documentation for this section has not yet been entered.
Documentation for this section has not yet been entered.


The System.Collections.ObjectModel.KeyedCollection`2 class provides both O(1) indexed retrieval and keyed retrieval that approaches O(1). It is an abstract type, or more accurately an infinite set of abstract types, because each of its constructed generic types is an abstract base class. To use System.Collections.ObjectModel.KeyedCollection`2, derive your collection type from the appropriate constructed type.

The System.Collections.ObjectModel.KeyedCollection`2 class is a hybrid between a collection based on the IList`1 generic interface and a collection based on the IDictionary`2 generic interface. Like collections based on the IList`1 generic interface, System.Collections.ObjectModel.KeyedCollection`2 is an indexed list of items. Like collections based on the IDictionary`2 generic interface, System.Collections.ObjectModel.KeyedCollection`2 has a key associated with each element.

Unlike dictionaries, an element of System.Collections.ObjectModel.KeyedCollection`2 is not a key/value pair; instead, the entire element is the value and the key is embedded within the value. For example, an element of a collection derived from KeyedCollection<String,String> (KeyedCollection(Of String, String) in Visual Basic) might be "John Doe Jr." where the value is "John Doe Jr." and the key is "Doe"; or a collection of employee records containing integer keys could be derived from KeyedCollection<int,Employee>. The abstract KeyedCollection`2.GetKeyForItem(`1) method extracts the key from the element.

By default, the System.Collections.ObjectModel.KeyedCollection`2 includes a lookup dictionary that you can obtain with the KeyedCollection`2.Dictionary property. When an item is added to the System.Collections.ObjectModel.KeyedCollection`2, the item's key is extracted once and saved in the lookup dictionary for faster searches. This behavior is overridden by specifying a dictionary creation threshold when you create the System.Collections.ObjectModel.KeyedCollection`2. The lookup dictionary is created the first time the number of elements exceeds that threshold. If you specify –1 as the threshold, the lookup dictionary is never created.


When the internal lookup dictionary is used, it contains references to all the items in the collection if TItem is a reference type, or copies of all the items in the collection if TItem is a value type. Thus, using the lookup dictionary may not be appropriate if TItem is a value type.

You can access an item by its index or key by using the KeyedCollection`2.Item(`0) property. You can add items without a key, but these items can subsequently be accessed only by index.


Namespace: System.Collections.ObjectModel
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions:,
Since: .NET 2.0