Прочетете на английски Редактиране

Споделяне чрез


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.

C#
public SortedDictionary();

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.

C#
// 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.");
}

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

.NET 10 и други версии
Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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.

C#
public SortedDictionary(System.Collections.Generic.IComparer<TKey> comparer);
C#
public SortedDictionary(System.Collections.Generic.IComparer<TKey>? comparer);

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.

C#
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
 */

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

.NET 10 и други версии
Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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.

C#
public SortedDictionary(System.Collections.Generic.IDictionary<TKey,TValue> dictionary);

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.

C#
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
 */

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

.NET 10 и други версии
Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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.

C#
public SortedDictionary(System.Collections.Generic.IDictionary<TKey,TValue> dictionary, System.Collections.Generic.IComparer<TKey> comparer);
C#
public SortedDictionary(System.Collections.Generic.IDictionary<TKey,TValue> dictionary, System.Collections.Generic.IComparer<TKey>? comparer);

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.

C#
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
 */

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

.NET 10 и други версии
Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0