Partager via


KeyedCollection<TKey, TItem> Constructor (IEqualityComparer<TKey>, Int32)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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)

Syntax

'Declaration
Protected Sub New ( _
    comparer As IEqualityComparer(Of TKey), _
    dictionaryCreationThreshold As Integer _
)
protected KeyedCollection(
    IEqualityComparer<TKey> comparer,
    int dictionaryCreationThreshold
)

Parameters

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

Exceptions

Exception Condition
ArgumentOutOfRangeException

dictionaryCreationThreshold is less than –1.

Remarks

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.

NoteNote:

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.

Version Information

Silverlight

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

Platforms

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