KeyedCollection(Of TKey, TItem) Constructor (IEqualityComparer(Of TKey), Int32)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Initializes a new instance of the KeyedCollection(Of TKey, TItem) class that uses the specified equality comparer and creates a lookup dictionary when the specified threshold is exceeded.
Assembly: mscorlib (in mscorlib.dll)
'Declaration Protected Sub New ( _ comparer As IEqualityComparer(Of TKey), _ dictionaryCreationThreshold As Integer _ )
Parameters
- comparer
- Type: System.Collections.Generic.IEqualityComparer(Of TKey)
The implementation of the IEqualityComparer(Of T) generic interface to use when comparing keys, or Nothing to use the default equality comparer for the type of the key, obtained from EqualityComparer(Of T).Default.
- 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.
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException | dictionaryCreationThreshold is less than –1. |
By default, the KeyedCollection(Of TKey, TItem) includes a lookup dictionary that is created when the first item is added. When an item is added to the KeyedCollection(Of 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.
Note: |
|---|
Because the KeyedCollection(Of 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(Of 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.
Note: