SortedDictionary<TKey,TValue> 构造函数

定义

初始化 SortedDictionary<TKey,TValue> 类的新实例。

重载

SortedDictionary<TKey,TValue>()

初始化 SortedDictionary<TKey,TValue> 类的一个新实例,该实例为空并对键类型使用默认 IComparer<T> 实现。

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

初始化 SortedDictionary<TKey,TValue> 类的一个新实例,该实例为空并对比较键使用指定 IComparer<T> 实现。

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

初始化 SortedDictionary<TKey,TValue> 类的新实例,该实例包含从指定的 IDictionary<TKey,TValue> 中复制的元素,并使用键类型的默认 IComparer<T> 实现。

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

初始化 SortedDictionary<TKey,TValue> 类的新实例,该实例包含从指定的 IDictionary<TKey,TValue> 中复制的元素,并使用指定的 IComparer<T> 实现来比较键。

SortedDictionary<TKey,TValue>()

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

初始化 SortedDictionary<TKey,TValue> 类的一个新实例,该实例为空并对键类型使用默认 IComparer<T> 实现。

public SortedDictionary ();

示例

下面的代码示例使用字符串键创建一个空 SortedDictionary<TKey,TValue> 字符串,并使用 Add 方法添加一些元素。 该示例演示方法在 Add 尝试添加重复键时引发 ArgumentException

此代码示例是为 SortedDictionary<TKey,TValue> 类提供的一个更大示例的一部分。

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

注解

根据默认比较器, SortedDictionary<TKey,TValue> 中的每个键都必须是唯一的。

SortedDictionary<TKey,TValue> 需要比较器实现来执行键比较。 此构造函数使用默认的泛型相等比较器 Comparer<T>.Default。 如果 类型 TKey 实现 System.IComparable<T> 泛型接口,则默认比较器使用该实现。 或者,可以使用接受 参数的 IComparer<T> 构造函数指定泛型接口的 comparer 实现。

此构造函数是 O (1) 操作。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.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
.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

初始化 SortedDictionary<TKey,TValue> 类的一个新实例,该实例为空并对比较键使用指定 IComparer<T> 实现。

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

参数

comparer
IComparer<TKey>

比较键时要使用的 IComparer<T> 实现,或者为 null,以便为键类型使用默认的 Comparer<T>

示例

下面的代码示例使用当前区域性不区分大小写的比较器创建 SortedDictionary<TKey,TValue> 。 该示例添加了四个元素,其中一些具有小写键,一些元素使用大写键。 然后,该示例尝试添加一个元素,该元素的键仅在大小写上与现有键不同,捕获生成的异常并显示错误消息。 最后,该示例按不区分大小写的排序顺序显示元素。

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

注解

根据指定的比较器, SortedDictionary<TKey,TValue> 中的每个键都必须是唯一的。

SortedDictionary<TKey,TValue> 需要比较器实现来执行键比较。 如果 comparernull,则此构造函数使用默认的泛型相等比较器 Comparer<T>.Default。 如果 类型 TKey 实现 System.IComparable<T> 泛型接口,则默认比较器使用该实现。

此构造函数是 O (1) 操作。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.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
.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

初始化 SortedDictionary<TKey,TValue> 类的新实例,该实例包含从指定的 IDictionary<TKey,TValue> 中复制的元素,并使用键类型的默认 IComparer<T> 实现。

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

参数

例外

dictionarynull

dictionary 包含一个或多个重复键。

示例

下面的代码示例演示如何使用 SortedDictionary<TKey,TValue> 通过将 传递给Dictionary<TKey,TValue>SortedDictionary<TKey,TValue>(IComparer<TKey>)构造函数,在 中创建Dictionary<TKey,TValue>信息的排序副本。

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

注解

根据默认比较器,中的每个键都必须是唯一 SortedDictionary<TKey,TValue> 的;因此,根据默认比较器,源 dictionary 中的每个键也必须是唯一的。

SortedDictionary<TKey,TValue> 需要比较器实现来执行键比较。 此构造函数使用默认的泛型相等比较器 Comparer<T>.Default。 如果 类型 TKey 实现 System.IComparable<T> 泛型接口,则默认比较器使用该实现。 或者,可以使用接受 参数的 IComparer<T> 构造函数指定泛型接口的 comparer 实现。

此构造函数是 O (n 日志 n) 操作,其中 n 是 中的 dictionary元素数。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.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
.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

初始化 SortedDictionary<TKey,TValue> 类的新实例,该实例包含从指定的 IDictionary<TKey,TValue> 中复制的元素,并使用指定的 IComparer<T> 实现来比较键。

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);

参数

comparer
IComparer<TKey>

比较键时要使用的 IComparer<T> 实现,或者为 null,以便为键类型使用默认的 Comparer<T>

例外

dictionarynull

dictionary 包含一个或多个重复键。

示例

下面的代码示例演示如何使用 SortedDictionary<TKey,TValue> 通过将 传递给Dictionary<TKey,TValue>SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>)构造函数,在不区分大小写的 中创建信息不区分大小写Dictionary<TKey,TValue>的排序副本。 在此示例中,不区分大小写的比较器适用于当前区域性。

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

注解

根据指定的比较器,中的每个键都必须是唯一 SortedDictionary<TKey,TValue> 的;因此,根据指定的比较器,源 dictionary 中的每个键也必须是唯一的。

SortedDictionary<TKey,TValue> 需要比较器实现来执行键比较。 如果 comparernull,则此构造函数使用默认的泛型相等比较器 Comparer<T>.Default。 如果 类型 TKey 实现 System.IComparable<T> 泛型接口,则默认比较器使用该实现。

此构造函数是 O (n 日志 n) 操作,其中 n 是 中的 dictionary元素数。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.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
.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