Rediger

Del via


SortedDictionary<TKey,TValue> Constructors

Definition

Initializes a new instance of the SortedDictionary<TKey,TValue> class.

Overloads

SortedDictionary<TKey,TValue>()

Initializes a new instance of the SortedDictionary<TKey,TValue> class that is empty and uses the default IComparer<T> implementation for the key type.

SortedDictionary<TKey,TValue>(IComparer<TKey>)

Initializes a new instance of the SortedDictionary<TKey,TValue> class that is empty and uses the specified IComparer<T> implementation to compare keys.

SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>)

Initializes a new instance of the SortedDictionary<TKey,TValue> class that contains elements copied from the specified IDictionary<TKey,TValue> and uses the default IComparer<T> implementation for the key type.

SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>)

Initializes a new instance of the SortedDictionary<TKey,TValue> class that contains elements copied from the specified IDictionary<TKey,TValue> and uses the specified IComparer<T> implementation to compare keys.

SortedDictionary<TKey,TValue>()

Source:
SortedDictionary.cs
Source:
SortedDictionary.cs
Source:
SortedDictionary.cs

Initializes a new instance of the SortedDictionary<TKey,TValue> class that is empty and uses the default IComparer<T> implementation for the key type.

public:
 SortedDictionary();
public SortedDictionary ();
Public Sub New ()

Examples

The following code example creates an empty SortedDictionary<TKey,TValue> of strings with string keys and uses the Add method to add some elements. The example demonstrates that the Add method throws an ArgumentException when attempting to add a duplicate key.

This code example is part of a larger example provided for the SortedDictionary<TKey,TValue> class.

// Create a new sorted dictionary of strings, with string
// keys.
SortedDictionary<string, string> openWith =
    new SortedDictionary<string, string>();

// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");

// The Add method throws an exception if the new key is
// already in the dictionary.
try
{
    openWith.Add("txt", "winword.exe");
}
catch (ArgumentException)
{
    Console.WriteLine("An element with Key = \"txt\" already exists.");
}
' Create a new sorted dictionary of strings, with string 
' keys. 
Dim openWith As New SortedDictionary(Of String, String)

' Add some elements to the dictionary. There are no 
' duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe")
openWith.Add("bmp", "paint.exe")
openWith.Add("dib", "paint.exe")
openWith.Add("rtf", "wordpad.exe")

' The Add method throws an exception if the new key is 
' already in the dictionary.
Try
    openWith.Add("txt", "winword.exe")
Catch 
    Console.WriteLine("An element with Key = ""txt"" already exists.")
End Try

Remarks

Every key in a SortedDictionary<TKey,TValue> must be unique according to the default comparer.

SortedDictionary<TKey,TValue> requires a comparer implementation to perform key comparisons. This constructor uses the default generic equality comparer Comparer<T>.Default. If type TKey implements the System.IComparable<T> generic interface, the default comparer uses that implementation. Alternatively, you can specify an implementation of the IComparer<T> generic interface by using a constructor that accepts a comparer parameter.

This constructor is an O(1) operation.

See also

Applies to

SortedDictionary<TKey,TValue>(IComparer<TKey>)

Source:
SortedDictionary.cs
Source:
SortedDictionary.cs
Source:
SortedDictionary.cs

Initializes a new instance of the SortedDictionary<TKey,TValue> class that is empty and uses the specified IComparer<T> implementation to compare keys.

public:
 SortedDictionary(System::Collections::Generic::IComparer<TKey> ^ comparer);
public SortedDictionary (System.Collections.Generic.IComparer<TKey> comparer);
public SortedDictionary (System.Collections.Generic.IComparer<TKey>? comparer);
new System.Collections.Generic.SortedDictionary<'Key, 'Value> : System.Collections.Generic.IComparer<'Key> -> System.Collections.Generic.SortedDictionary<'Key, 'Value>
Public Sub New (comparer As IComparer(Of TKey))

Parameters

comparer
IComparer<TKey>

The IComparer<T> implementation to use when comparing keys, or null to use the default Comparer<T> for the type of the key.

Examples

The following code example creates a SortedDictionary<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.

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new SortedDictionary of strings, with string keys
        // and a case-insensitive comparer for the current culture.
        SortedDictionary<string, string> openWith =
                      new SortedDictionary<string, string>(
                          StringComparer.CurrentCultureIgnoreCase);

        // Add some elements to the dictionary.
        openWith.Add("txt", "notepad.exe");
        openWith.Add("bmp", "paint.exe");
        openWith.Add("DIB", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");

        // Try to add a fifth element with a key that is the same
        // except for case; this would be allowed with the default
        // comparer.
        try
        {
            openWith.Add("BMP", "paint.exe");
        }
        catch (ArgumentException)
        {
            Console.WriteLine("\nBMP is already in the dictionary.");
        }

        // List the contents of the sorted dictionary.
        Console.WriteLine();
        foreach( KeyValuePair<string, string> kvp in openWith )
        {
            Console.WriteLine("Key = {0}, Value = {1}", kvp.Key,
                kvp.Value);
        }
    }
}

/* This code example produces the following output:

BMP is already in the dictionary.

Key = bmp, Value = paint.exe
Key = DIB, Value = paint.exe
Key = rtf, Value = wordpad.exe
Key = txt, Value = notepad.exe
 */
Imports System.Collections.Generic

Public Class Example
    
    Public Shared Sub Main() 

        ' Create a new SortedDictionary of strings, with string keys 
        ' and a case-insensitive comparer for the current culture.
        Dim openWith As New SortedDictionary(Of String, String)( _
            StringComparer.CurrentCultureIgnoreCase)
        
        ' Add some elements to the dictionary. 
        openWith.Add("txt", "notepad.exe")
        openWith.Add("bmp", "paint.exe")
        openWith.Add("DIB", "paint.exe")
        openWith.Add("rtf", "wordpad.exe")

        ' Try to add a fifth element with a key that is the same 
        ' except for case; this would be allowed with the default
        ' comparer.
        Try
            openWith.Add("BMP", "paint.exe")
        Catch ex As ArgumentException
            Console.WriteLine(vbLf & "BMP is already in the dictionary.")
        End Try
        
        ' List the contents of the sorted dictionary.
        Console.WriteLine()
        For Each kvp As KeyValuePair(Of String, String) In openWith
            Console.WriteLine("Key = {0}, Value = {1}", _
                kvp.Key, kvp.Value)
        Next kvp

    End Sub

End Class

' This code example produces the following output:
'
'BMP is already in the dictionary.
'
'Key = bmp, Value = paint.exe
'Key = DIB, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe

Remarks

Every key in a SortedDictionary<TKey,TValue> must be unique according to the specified comparer.

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

This constructor is an O(1) operation.

See also

Applies to

SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>)

Source:
SortedDictionary.cs
Source:
SortedDictionary.cs
Source:
SortedDictionary.cs

Initializes a new instance of the SortedDictionary<TKey,TValue> class that contains elements copied from the specified IDictionary<TKey,TValue> and uses the default IComparer<T> implementation for the key type.

public:
 SortedDictionary(System::Collections::Generic::IDictionary<TKey, TValue> ^ dictionary);
public SortedDictionary (System.Collections.Generic.IDictionary<TKey,TValue> dictionary);
new System.Collections.Generic.SortedDictionary<'Key, 'Value> : System.Collections.Generic.IDictionary<'Key, 'Value> -> System.Collections.Generic.SortedDictionary<'Key, 'Value>
Public Sub New (dictionary As IDictionary(Of TKey, TValue))

Parameters

dictionary
IDictionary<TKey,TValue>

The IDictionary<TKey,TValue> whose elements are copied to the new SortedDictionary<TKey,TValue>.

Exceptions

dictionary is null.

dictionary contains one or more duplicate keys.

Examples

The following code example shows how to use SortedDictionary<TKey,TValue> to create a sorted copy of the information in a Dictionary<TKey,TValue>, by passing the Dictionary<TKey,TValue> to the SortedDictionary<TKey,TValue>(IComparer<TKey>) constructor.

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new Dictionary of strings, with string keys.
        //
        Dictionary<string, string> openWith =
                                  new Dictionary<string, string>();

        // Add some elements to the dictionary.
        openWith.Add("txt", "notepad.exe");
        openWith.Add("bmp", "paint.exe");
        openWith.Add("dib", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");

        // Create a SortedDictionary of strings with string keys,
        // and initialize it with the contents of the Dictionary.
        SortedDictionary<string, string> copy =
                  new SortedDictionary<string, string>(openWith);

        // List the contents of the copy.
        Console.WriteLine();
        foreach( KeyValuePair<string, string> kvp in copy )
        {
            Console.WriteLine("Key = {0}, Value = {1}",
               kvp.Key, kvp.Value);
        }
    }
}

/* This code example produces the following output:

Key = bmp, Value = paint.exe
Key = dib, Value = paint.exe
Key = rtf, Value = wordpad.exe
Key = txt, Value = notepad.exe
 */
Imports System.Collections.Generic

Public Class Example
    
    Public Shared Sub Main() 

        ' Create a new Dictionary of strings, with string 
        ' keys.
        Dim openWith As New Dictionary(Of String, String)
        
        ' Add some elements to the dictionary. 
        openWith.Add("txt", "notepad.exe")
        openWith.Add("bmp", "paint.exe")
        openWith.Add("dib", "paint.exe")
        openWith.Add("rtf", "wordpad.exe")
        
        ' Create a SortedDictionary of strings with string keys, 
        ' and initialize it with the contents of the Dictionary.
        Dim copy As New SortedDictionary(Of String, String)(openWith)

        ' List the sorted contents of the copy.
        Console.WriteLine()
        For Each kvp As KeyValuePair(Of String, String) In copy
            Console.WriteLine("Key = {0}, Value = {1}", _
                kvp.Key, kvp.Value)
        Next kvp

    End Sub

End Class

' This code example produces the following output:
'
'Key = bmp, Value = paint.exe
'Key = dib, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe

Remarks

Every key in a SortedDictionary<TKey,TValue> must be unique according to the default comparer; therefore, every key in the source dictionary must also be unique according to the default comparer.

SortedDictionary<TKey,TValue> requires a comparer implementation to perform key comparisons. This constructor uses the default generic equality comparer, Comparer<T>.Default. If type TKey implements the System.IComparable<T> generic interface, the default comparer uses that implementation. Alternatively, you can specify an implementation of the IComparer<T> generic interface by using a constructor that accepts a comparer parameter.

This constructor is an O(n log n) operation, where n is the number of elements in dictionary.

See also

Applies to

SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>)

Source:
SortedDictionary.cs
Source:
SortedDictionary.cs
Source:
SortedDictionary.cs

Initializes a new instance of the SortedDictionary<TKey,TValue> class that contains elements copied from the specified IDictionary<TKey,TValue> and uses the specified IComparer<T> implementation to compare keys.

public:
 SortedDictionary(System::Collections::Generic::IDictionary<TKey, TValue> ^ dictionary, System::Collections::Generic::IComparer<TKey> ^ comparer);
public SortedDictionary (System.Collections.Generic.IDictionary<TKey,TValue> dictionary, System.Collections.Generic.IComparer<TKey> comparer);
public SortedDictionary (System.Collections.Generic.IDictionary<TKey,TValue> dictionary, System.Collections.Generic.IComparer<TKey>? comparer);
new System.Collections.Generic.SortedDictionary<'Key, 'Value> : System.Collections.Generic.IDictionary<'Key, 'Value> * System.Collections.Generic.IComparer<'Key> -> System.Collections.Generic.SortedDictionary<'Key, 'Value>
Public Sub New (dictionary As IDictionary(Of TKey, TValue), comparer As IComparer(Of TKey))

Parameters

dictionary
IDictionary<TKey,TValue>

The IDictionary<TKey,TValue> whose elements are copied to the new SortedDictionary<TKey,TValue>.

comparer
IComparer<TKey>

The IComparer<T> implementation to use when comparing keys, or null to use the default Comparer<T> for the type of the key.

Exceptions

dictionary is null.

dictionary contains one or more duplicate keys.

Examples

The following code example shows how to use SortedDictionary<TKey,TValue> to create a case-insensitive sorted copy of the information in a case-insensitive Dictionary<TKey,TValue>, by passing the Dictionary<TKey,TValue> to the SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) constructor. In this example, the case-insensitive comparers are for the current culture.

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new Dictionary of strings, with string keys and
        // a case-insensitive equality comparer for the current
        // culture.
        Dictionary<string, string> openWith =
            new Dictionary<string, string>
                (StringComparer.CurrentCultureIgnoreCase);

        // Add some elements to the dictionary.
        openWith.Add("txt", "notepad.exe");
        openWith.Add("Bmp", "paint.exe");
        openWith.Add("DIB", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");

        // List the contents of the Dictionary.
        Console.WriteLine();
        foreach( KeyValuePair<string, string> kvp in openWith)
        {
            Console.WriteLine("Key = {0}, Value = {1}", kvp.Key,
                kvp.Value);
        }

        // Create a SortedDictionary of strings with string keys and a
        // case-insensitive equality comparer for the current culture,
        // and initialize it with the contents of the Dictionary.
        SortedDictionary<string, string> copy =
                    new SortedDictionary<string, string>(openWith,
                        StringComparer.CurrentCultureIgnoreCase);

        // List the sorted contents of the copy.
        Console.WriteLine();
        foreach( KeyValuePair<string, string> kvp in copy )
        {
            Console.WriteLine("Key = {0}, Value = {1}", kvp.Key,
                kvp.Value);
        }
    }
}

/* This code example produces the following output:

Key = txt, Value = notepad.exe
Key = Bmp, Value = paint.exe
Key = DIB, Value = paint.exe
Key = rtf, Value = wordpad.exe

Key = Bmp, Value = paint.exe
Key = DIB, Value = paint.exe
Key = rtf, Value = wordpad.exe
Key = txt, Value = notepad.exe
 */
Imports System.Collections.Generic

Public Class Example
    
    Public Shared Sub Main() 

        ' Create a new Dictionary of strings, with string keys and
        ' a case-insensitive equality comparer for the current 
        ' culture.
        Dim openWith As New Dictionary(Of String, String)( _
            StringComparer.CurrentCultureIgnoreCase)
        
        ' Add some elements to the dictionary. 
        openWith.Add("txt", "notepad.exe")
        openWith.Add("Bmp", "paint.exe")
        openWith.Add("DIB", "paint.exe")
        openWith.Add("rtf", "wordpad.exe")
        
        ' List the contents of the Dictionary.
        Console.WriteLine()
        For Each kvp As KeyValuePair(Of String, String) In openWith
            Console.WriteLine("Key = {0}, Value = {1}", _
                kvp.Key, kvp.Value)
        Next kvp

        ' Create a SortedDictionary of strings with string keys and a 
        ' case-insensitive equality comparer for the current culture,
        ' and initialize it with the contents of the Dictionary.
        Dim copy As New SortedDictionary(Of String, String)(openWith, _
            StringComparer.CurrentCultureIgnoreCase)

        ' List the sorted contents of the copy.
        Console.WriteLine()
        For Each kvp As KeyValuePair(Of String, String) In copy
            Console.WriteLine("Key = {0}, Value = {1}", _
                kvp.Key, kvp.Value)
        Next kvp

    End Sub

End Class

' This code example produces the following output:
'
'Key = txt, Value = notepad.exe
'Key = Bmp, Value = paint.exe
'Key = DIB, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'
'Key = Bmp, Value = paint.exe
'Key = DIB, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe

Remarks

Every key in a SortedDictionary<TKey,TValue> must be unique according to the specified comparer; therefore, every key in the source dictionary must also be unique according to the specified comparer.

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

This constructor is an O(n log n) operation, where n is the number of elements in dictionary.

See also

Applies to