SortedDictionary(TKey, TValue) Constructor (IComparer(TKey))

SortedDictionary(Of TKey, TValue) Constructor (IComparer(Of TKey))

[ 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 SortedDictionary(Of TKey, TValue) class that is empty and uses the specified IComparer(Of T) implementation to compare keys.

Namespace:  System.Collections.Generic
Assembly:  System (in System.dll)

'Declaration
Public Sub New ( _
	comparer As IComparer(Of TKey) _
)

Parameters

comparer
Type: System.Collections.Generic.IComparer(Of TKey)
The IComparer(Of T) implementation to use when comparing keys, or Nothing to use the default Comparer(Of T) for the type of the key.

Every key in a SortedDictionary(Of TKey, TValue) must be unique according to the specified comparer.

SortedDictionary(Of TKey, TValue) requires a comparer implementation to perform key comparisons. If comparer is Nothing, this constructor uses the default generic equality comparer, Comparer(Of T).Default. If type TKey implements the System.IComparable(Of T) generic interface, the default comparer uses that implementation.

This constructor is an O(1) operation.

The following code example creates a SortedDictionary(Of TKey, TValue) with a case-insensitive comparer for the current culture. The example adds four elements, some with lower-case keys and some with upper-case keys. The example then attempts to add an element with a key that differs from an existing key only by case, catches the resulting exception, and displays an error message. Finally, the example displays the elements in case-insensitive sort order.

Windows Phone OS

Supported in: 8.1, 8.0

Show:
© 2017 Microsoft