SortedList<TKey,TValue> 构造函数

定义

初始化 SortedList<TKey,TValue> 类的新实例。Initializes a new instance of the SortedList<TKey,TValue> class.

重载

SortedList<TKey,TValue>()

初始化 SortedList<TKey,TValue> 类的新实例,该示例为空且具有默认的初始容量,并使用默认的 IComparer<T>Initializes a new instance of the SortedList<TKey,TValue> class that is empty, has the default initial capacity, and uses the default IComparer<T>.

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

初始化 SortedList<TKey,TValue> 类的新实例,该实例为空,具有默认的初始容量并使用指定的 IComparer<T>Initializes a new instance of the SortedList<TKey,TValue> class that is empty, has the default initial capacity, and uses the specified IComparer<T>.

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

初始化 SortedList<TKey,TValue> 类的新实例,该实例包含从指定的 IDictionary<TKey,TValue> 中复制的元素,其容量足以容纳所复制的元素数并使用默认的 IComparer<T>Initializes a new instance of the SortedList<TKey,TValue> class that contains elements copied from the specified IDictionary<TKey,TValue>, has sufficient capacity to accommodate the number of elements copied, and uses the default IComparer<T>.

SortedList<TKey,TValue>(Int32)

初始化 SortedList<TKey,TValue> 类的新实例,该示例为空且具有指定的初始容量,并使用默认的 IComparer<T>Initializes a new instance of the SortedList<TKey,TValue> class that is empty, has the specified initial capacity, and uses the default IComparer<T>.

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

初始化 SortedList<TKey,TValue> 类的新实例,该实例包含从指定的 IDictionary<TKey,TValue> 中复制的元素,其容量足以容纳所复制的元素数并使用指定的 IComparer<T>Initializes a new instance of the SortedList<TKey,TValue> class that contains elements copied from the specified IDictionary<TKey,TValue>, has sufficient capacity to accommodate the number of elements copied, and uses the specified IComparer<T>.

SortedList<TKey,TValue>(Int32, IComparer<TKey>)

初始化 SortedList<TKey,TValue> 类的新实例,该实例为空,具有指定的初始容量并使用指定的 IComparer<T>Initializes a new instance of the SortedList<TKey,TValue> class that is empty, has the specified initial capacity, and uses the specified IComparer<T>.

SortedList<TKey,TValue>()

初始化 SortedList<TKey,TValue> 类的新实例,该示例为空且具有默认的初始容量,并使用默认的 IComparer<T>Initializes a new instance of the SortedList<TKey,TValue> class that is empty, has the default initial capacity, and uses the default IComparer<T>.

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

示例

下面的代码示例 SortedList<TKey,TValue> 使用字符串键创建一个空的字符串,并使用 Add 方法来添加一些元素。The following code example creates an empty SortedList<TKey,TValue> of strings with string keys and uses the Add method to add some elements. 该示例演示了 Add ArgumentException 在尝试添加重复键时方法引发。The example demonstrates that the Add method throws an ArgumentException when attempting to add a duplicate key.

此代码示例是为类提供的更大示例的一部分 SortedList<TKey,TValue>This code example is part of a larger example provided for the SortedList<TKey,TValue> class.

// Create a new sorted list of strings, with string
// keys.
SortedList<String^, String^>^ openWith =
    gcnew SortedList<String^, String^>();

// Add some elements to the list. 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 list.
try
{
    openWith->Add("txt", "winword.exe");
}
catch (ArgumentException^)
{
    Console::WriteLine("An element with Key = \"txt\" already exists.");
}
// Create a new sorted list of strings, with string
// keys.
SortedList<string, string> openWith =
    new SortedList<string, string>();

// Add some elements to the list. 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 list.
try
{
    openWith.Add("txt", "winword.exe");
}
catch (ArgumentException)
{
    Console.WriteLine("An element with Key = \"txt\" already exists.");
}
' Create a new sorted list of strings, with string 
' keys. 
Dim openWith As New SortedList(Of String, String)

' Add some elements to the list. 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 list.
Try
    openWith.Add("txt", "winword.exe")
Catch 
    Console.WriteLine("An element with Key = ""txt"" already exists.")
End Try

注解

根据默认的比较器,中的每个键都 SortedList<TKey,TValue> 必须是唯一的。Every key in a SortedList<TKey,TValue> must be unique according to the default comparer.

此构造函数使用的初始容量的默认值 SortedList<TKey,TValue>This constructor uses the default value for the initial capacity of the SortedList<TKey,TValue>. 若要设置初始容量,请使用 SortedList<TKey,TValue>(Int32) 构造函数。To set the initial capacity, use the SortedList<TKey,TValue>(Int32) constructor. 如果可以估计集合的最终大小,则指定初始容量就不必在向中添加元素时执行多个大小调整操作 SortedList<TKey,TValue>If the final size of the collection can be estimated, specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the SortedList<TKey,TValue>.

此构造函数使用的默认比较器 TKeyThis constructor uses the default comparer for TKey. 若要指定比较器,请使用 SortedList<TKey,TValue>(IComparer<TKey>) 构造函数。To specify a comparer, use the SortedList<TKey,TValue>(IComparer<TKey>) constructor. 默认比较器 Comparer<T>.Default 将检查键类型是否 TKey 实现 System.IComparable<T> 并使用该实现(如果可用)。The default comparer Comparer<T>.Default checks whether the key type TKey implements System.IComparable<T> and uses that implementation, if available. 如果不是,则 Comparer<T>.Default 检查键类型是否 TKey 实现 System.IComparableIf not, Comparer<T>.Default checks whether the key type TKey implements System.IComparable. 如果键类型 TKey 未实现任何一个接口,可以 System.Collections.Generic.IComparer<T> 在接受参数的构造函数重载中指定实现 comparerIf the key type TKey does not implement either interface, you can specify a System.Collections.Generic.IComparer<T> implementation in a constructor overload that accepts a comparer parameter.

此构造函数是一个 O (1) 操作。This constructor is an O(1) operation.

另请参阅

适用于

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

初始化 SortedList<TKey,TValue> 类的新实例,该实例为空,具有默认的初始容量并使用指定的 IComparer<T>Initializes a new instance of the SortedList<TKey,TValue> class that is empty, has the default initial capacity, and uses the specified IComparer<T>.

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

参数

comparer
IComparer<TKey>

在对键进行比较时使用的 IComparer<T> 实现。The IComparer<T> implementation to use when comparing keys.

- 或 --or- null,则为这类键使用默认的 Comparer<T>null to use the default Comparer<T> for the type of the key.

示例

下面的代码示例创建一个排序列表,其中包含当前区域性的不区分大小写的比较器。The following code example creates a sorted list 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 sorted list of strings, with string keys and
        // a case-insensitive comparer for the current culture.
        SortedList<string, string> openWith =
                      new SortedList<string, string>(
                          StringComparer.CurrentCultureIgnoreCase);

        // Add some elements to the list.
        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 sorted list.");
        }

        // List the contents of the sorted list.
        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 sorted list.

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 sorted list of strings, with string keys and
        ' a case-insensitive comparer for the current culture.
        Dim openWith As New SortedList(Of String, String)( _
            StringComparer.CurrentCultureIgnoreCase)
        
        ' Add some elements to the list. 
        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 sorted list.")
        End Try
        
        ' List the contents of the sorted list.
        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 sorted list.
'
'Key = bmp, Value = paint.exe
'Key = DIB, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe

注解

根据指定的比较器,中的每个键都 SortedList<TKey,TValue> 必须是唯一的。Every key in a SortedList<TKey,TValue> must be unique according to the specified comparer.

此构造函数使用的初始容量的默认值 SortedList<TKey,TValue>This constructor uses the default value for the initial capacity of the SortedList<TKey,TValue>. 若要设置初始容量,请使用 SortedList<TKey,TValue>(Int32, IComparer<TKey>) 构造函数。To set the initial capacity, use the SortedList<TKey,TValue>(Int32, IComparer<TKey>) constructor. 如果可以估计集合的最终大小,则指定初始容量就不必在向中添加元素时执行多个大小调整操作 SortedList<TKey,TValue>If the final size of the collection can be estimated, specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the SortedList<TKey,TValue>.

此构造函数是一个 O (1) 操作。This constructor is an O(1) operation.

另请参阅

适用于

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

初始化 SortedList<TKey,TValue> 类的新实例,该实例包含从指定的 IDictionary<TKey,TValue> 中复制的元素,其容量足以容纳所复制的元素数并使用默认的 IComparer<T>Initializes a new instance of the SortedList<TKey,TValue> class that contains elements copied from the specified IDictionary<TKey,TValue>, has sufficient capacity to accommodate the number of elements copied, and uses the default IComparer<T>.

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

参数

dictionary
IDictionary<TKey,TValue>

IDictionary<TKey,TValue>,它的元素被复制到新 SortedList<TKey,TValue>The IDictionary<TKey,TValue> whose elements are copied to the new SortedList<TKey,TValue>.

例外

dictionarynulldictionary is null.

dictionary 包含一个或多个重复键。dictionary contains one or more duplicate keys.

示例

下面的代码示例演示如何通过将 SortedList<TKey,TValue> Dictionary<TKey,TValue> 传递 Dictionary<TKey,TValue> 到构造函数来使用在中创建信息的已排序副本 SortedList<TKey,TValue>(IDictionary<TKey,TValue>)The following code example shows how to use SortedList<TKey,TValue> to create a sorted copy of the information in a Dictionary<TKey,TValue>, by passing the Dictionary<TKey,TValue> to the SortedList<TKey,TValue>(IDictionary<TKey,TValue>) 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 SortedList of strings with string keys,
        // and initialize it with the contents of the Dictionary.
        SortedList<string, string> copy =
                  new SortedList<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 SortedList of strings with string keys, 
        ' and initialize it with the contents of the Dictionary.
        Dim copy As New SortedList(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

注解

SortedList<TKey,TValue>根据默认的比较器,中的每个键都必须是唯一的; 同样, dictionary 根据默认的比较器,源中的每个键都必须是唯一的。Every key in a SortedList<TKey,TValue> must be unique according to the default comparer; likewise, every key in the source dictionary must also be unique according to the default comparer.

新的容量 SortedList<TKey,TValue> 设置为中的元素数 dictionary ,因此在填充列表时,不会发生调整大小。The capacity of the new SortedList<TKey,TValue> is set to the number of elements in dictionary, so no resizing takes place while the list is being populated.

此构造函数使用的默认比较器 TKeyThis constructor uses the default comparer for TKey. 若要指定比较器,请使用 SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) 构造函数。To specify a comparer, use the SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) constructor. 默认比较器 Comparer<T>.Default 将检查键类型是否 TKey 实现 System.IComparable<T> 并使用该实现(如果可用)。The default comparer Comparer<T>.Default checks whether the key type TKey implements System.IComparable<T> and uses that implementation, if available. 如果不是,则 Comparer<T>.Default 检查键类型是否 TKey 实现 System.IComparableIf not, Comparer<T>.Default checks whether the key type TKey implements System.IComparable. 如果键类型 TKey 未实现任何一个接口,可以 System.Collections.Generic.IComparer<T> 在接受参数的构造函数重载中指定实现 comparerIf the key type TKey does not implement either interface, you can specify a System.Collections.Generic.IComparer<T> implementation in a constructor overload that accepts a comparer parameter.

如果对中的数据 dictionary 进行排序,则此构造函数是一个 O (n) 操作,其中 n 是中的元素数 dictionaryIf the data in dictionary are sorted, this constructor is an O(n) operation, where n is the number of elements in dictionary. 否则,它是一个 O (n * n) 操作。Otherwise it is an O(n*n) operation.

另请参阅

适用于

SortedList<TKey,TValue>(Int32)

初始化 SortedList<TKey,TValue> 类的新实例,该示例为空且具有指定的初始容量,并使用默认的 IComparer<T>Initializes a new instance of the SortedList<TKey,TValue> class that is empty, has the specified initial capacity, and uses the default IComparer<T>.

public:
 SortedList(int capacity);
public SortedList (int capacity);
new System.Collections.Generic.SortedList<'Key, 'Value> : int -> System.Collections.Generic.SortedList<'Key, 'Value>
Public Sub New (capacity As Integer)

参数

capacity
Int32

SortedList<TKey,TValue> 可包含的初始元素数。The initial number of elements that the SortedList<TKey,TValue> can contain.

例外

capacity 小于零。capacity is less than zero.

示例

下面的代码示例创建一个初始容量为4的排序列表,并使用4个条目填充它。The following code example creates a sorted list with an initial capacity of 4 and populates it with 4 entries.

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new sorted list of strings, with string keys and
        // an initial capacity of 4.
        SortedList<string, string> openWith =
                               new SortedList<string, string>(4);

        // Add 4 elements to the list.
        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 sorted list.
        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:

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 sorted list of strings, with string keys and
        ' an initial capacity of 4.
        Dim openWith As New SortedList(Of String, String)(4)
        
        ' Add 4 elements to the list. 
        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 sorted list.
        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:
'
'Key = bmp, Value = paint.exe
'Key = dib, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe

注解

根据默认的比较器,中的每个键都 SortedList<TKey,TValue> 必须是唯一的。Every key in a SortedList<TKey,TValue> must be unique according to the default comparer.

的容量是在 SortedList<TKey,TValue> SortedList<TKey,TValue> 调整大小之前可包含的元素数。The capacity of a SortedList<TKey,TValue> is the number of elements that the SortedList<TKey,TValue> can hold before resizing. 当向添加元素时 SortedList<TKey,TValue> ,将根据需要通过重新分配内部数组来自动增加容量。As elements are added to a SortedList<TKey,TValue>, the capacity is automatically increased as required by reallocating the internal array.

如果集合的大小可为估算值,则指定初始容量后,无需在将元素添加到时执行多个大小调整操作 SortedList<TKey,TValue>If the size of the collection can be estimated, specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the SortedList<TKey,TValue>.

可以通过调用 TrimExcess 或显式设置属性来减少容量 CapacityThe capacity can be decreased by calling TrimExcess or by setting the Capacity property explicitly. 减少容量会重新分配内存,并复制中的所有元素 SortedList<TKey,TValue>Decreasing the capacity reallocates memory and copies all the elements in the SortedList<TKey,TValue>.

此构造函数使用的默认比较器 TKeyThis constructor uses the default comparer for TKey. 若要指定比较器,请使用 SortedList<TKey,TValue>(Int32, IComparer<TKey>) 构造函数。To specify a comparer, use the SortedList<TKey,TValue>(Int32, IComparer<TKey>) constructor. 默认比较器 Comparer<T>.Default 将检查键类型是否 TKey 实现 System.IComparable<T> 并使用该实现(如果可用)。The default comparer Comparer<T>.Default checks whether the key type TKey implements System.IComparable<T> and uses that implementation, if available. 如果不是,则 Comparer<T>.Default 检查键类型是否 TKey 实现 System.IComparableIf not, Comparer<T>.Default checks whether the key type TKey implements System.IComparable. 如果键类型 TKey 未实现任何一个接口,可以 System.Collections.Generic.IComparer<T> 在接受参数的构造函数重载中指定实现 comparerIf the key type TKey does not implement either interface, you can specify a System.Collections.Generic.IComparer<T> implementation in a constructor overload that accepts a comparer parameter.

此构造函数是一个 O (n) 操作,其中, ncapacityThis constructor is an O(n) operation, where n is capacity.

另请参阅

适用于

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

初始化 SortedList<TKey,TValue> 类的新实例,该实例包含从指定的 IDictionary<TKey,TValue> 中复制的元素,其容量足以容纳所复制的元素数并使用指定的 IComparer<T>Initializes a new instance of the SortedList<TKey,TValue> class that contains elements copied from the specified IDictionary<TKey,TValue>, has sufficient capacity to accommodate the number of elements copied, and uses the specified IComparer<T>.

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

参数

dictionary
IDictionary<TKey,TValue>

IDictionary<TKey,TValue>,它的元素被复制到新 SortedList<TKey,TValue>The IDictionary<TKey,TValue> whose elements are copied to the new SortedList<TKey,TValue>.

comparer
IComparer<TKey>

在对键进行比较时使用的 IComparer<T> 实现。The IComparer<T> implementation to use when comparing keys.

- 或 --or- null,则为这类键使用默认的 Comparer<T>null to use the default Comparer<T> for the type of the key.

例外

dictionarynulldictionary is null.

dictionary 包含一个或多个重复键。dictionary contains one or more duplicate keys.

示例

下面的代码示例演示如何通过将 SortedList<TKey,TValue> Dictionary<TKey,TValue> 传递 Dictionary<TKey,TValue> 到构造函数来使用来创建不区分大小写的信息的、不区分大小写的排序副本 SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>)The following code example shows how to use SortedList<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 SortedList<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");

        // Create a SortedList of strings with string keys and a
        // case-insensitive equality comparer for the current culture,
        // and initialize it with the contents of the Dictionary.
        SortedList<string, string> copy =
            new SortedList<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 = 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")
        
        ' Create a SortedList 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 SortedList(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 = Bmp, Value = paint.exe
'Key = DIB, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe

注解

SortedList<TKey,TValue>根据指定的比较器,中的每个键都必须是唯一的; 同样,根据指定的比较器,源中的每个键都 dictionary 必须是唯一的。Every key in a SortedList<TKey,TValue> must be unique according to the specified comparer; likewise, every key in the source dictionary must also be unique according to the specified comparer.

新的容量 SortedList<TKey,TValue> 设置为中的元素数 dictionary ,因此在填充列表时,不会发生调整大小。The capacity of the new SortedList<TKey,TValue> is set to the number of elements in dictionary, so no resizing takes place while the list is being populated.

如果对中的数据 dictionary 进行排序,则此构造函数是一个 O (n) 操作,其中 n 是中的元素数 dictionaryIf the data in dictionary are sorted, this constructor is an O(n) operation, where n is the number of elements in dictionary. 否则,它是一个 O (n * n) 操作。Otherwise it is an O(n*n) operation.

另请参阅

适用于

SortedList<TKey,TValue>(Int32, IComparer<TKey>)

初始化 SortedList<TKey,TValue> 类的新实例,该实例为空,具有指定的初始容量并使用指定的 IComparer<T>Initializes a new instance of the SortedList<TKey,TValue> class that is empty, has the specified initial capacity, and uses the specified IComparer<T>.

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

参数

capacity
Int32

SortedList<TKey,TValue> 可包含的初始元素数。The initial number of elements that the SortedList<TKey,TValue> can contain.

comparer
IComparer<TKey>

在对键进行比较时使用的 IComparer<T> 实现。The IComparer<T> implementation to use when comparing keys.

- 或 --or- null,则为这类键使用默认的 Comparer<T>null to use the default Comparer<T> for the type of the key.

例外

capacity 小于零。capacity is less than zero.

示例

下面的代码示例创建一个排序列表,其初始容量为5,并为当前区域性创建不区分大小写的比较器。The following code example creates a sorted list with an initial capacity of 5 and 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 sorted list of strings, with string keys, an
        // initial capacity of 5, and a case-insensitive comparer.
        SortedList<string, string> openWith =
                      new SortedList<string, string>(5,
                          StringComparer.CurrentCultureIgnoreCase);

        // Add 4 elements to the list.
        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 sorted list.");
        }

        // List the contents of the sorted list.
        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 sorted list.

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 sorted list of strings, with string keys, an
        ' initial capacity of 5, and a case-insensitive comparer.
        Dim openWith As New SortedList(Of String, String)(5, _
            StringComparer.CurrentCultureIgnoreCase)
        
        ' Add 4 elements to the list. 
        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 sorted list.")
        End Try
        
        ' List the contents of the sorted list.
        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 sorted list.
'
'Key = bmp, Value = paint.exe
'Key = DIB, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe

注解

根据指定的比较器,中的每个键都 SortedList<TKey,TValue> 必须是唯一的。Every key in a SortedList<TKey,TValue> must be unique according to the specified comparer.

的容量是在 SortedList<TKey,TValue> SortedList<TKey,TValue> 调整大小之前可包含的元素数。The capacity of a SortedList<TKey,TValue> is the number of elements that the SortedList<TKey,TValue> can hold before resizing. 当向添加元素时 SortedList<TKey,TValue> ,将根据需要通过重新分配内部数组来自动增加容量。As elements are added to a SortedList<TKey,TValue>, the capacity is automatically increased as required by reallocating the internal array.

如果集合的大小可为估算值,则指定初始容量后,无需在将元素添加到时执行多个大小调整操作 SortedList<TKey,TValue>If the size of the collection can be estimated, specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the SortedList<TKey,TValue>.

可以通过调用 TrimExcess 或显式设置属性来减少容量 CapacityThe capacity can be decreased by calling TrimExcess or by setting the Capacity property explicitly. 减少容量会重新分配内存,并复制中的所有元素 SortedList<TKey,TValue>Decreasing the capacity reallocates memory and copies all the elements in the SortedList<TKey,TValue>.

此构造函数是一个 O (n) 操作,其中, ncapacityThis constructor is an O(n) operation, where n is capacity.

另请参阅

适用于