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> Constructor (IEqualityComparer<TKey>, Int32)

Initializes a new instance of the KeyedCollection<TKey, TItem> class that uses the specified equality comparer and creates a lookup dictionary when the specified threshold is exceeded.

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

protected KeyedCollection(
	IEqualityComparer<TKey> comparer,
	int dictionaryCreationThreshold


Type: System.Collections.Generic.IEqualityComparer<TKey>
The implementation of the IEqualityComparer<T> generic interface to use when comparing keys, or null to use the default equality comparer for the type of the key, obtained from EqualityComparer<T>.Default.
Type: System.Int32
The number of elements the collection can hold without creating a lookup dictionary (0 creates the lookup dictionary when the first item is added), or –1 to specify that a lookup dictionary is never created.


dictionaryCreationThreshold is less than –1.

By default, the KeyedCollection<TKey, TItem> includes a lookup dictionary that is created when the first item is added. When an item is added to the KeyedCollection<TKey, TItem>, the item's key is extracted once and saved in the lookup dictionary for faster searches. This constructor allows you to override that behavior. Specify 0 to create the dictionary when the first element is added, 1 to create the dictionary when the second element is added, and so on. If you specify –1 as the threshold, the lookup dictionary is never created.

For very small collections the improvement in retrieval speed provided by the lookup dictionary might not be worth the extra memory required by the dictionary. Setting a threshold allows you to decide when to make that tradeoff.


Because the KeyedCollection<TKey, TItem> class is abstract (MustInherit in Visual Basic), you must derive from it in order to use it. In the constructor for your derived type, call the appropriate KeyedCollection<TKey, TItem> constructor. It is not necessary to expose functionality like the equality comparer or the dictionary creation threshold in your constructors.

This constructor is an O(1) operation.


Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

© 2015 Microsoft