SortedList<TKey,TValue> 构造函数

定义

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

重载

SortedList<TKey,TValue>()

初始化 SortedList<TKey,TValue> 类的新实例,该示例为空且具有默认的初始容量,并使用默认的 IComparer<T>

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

初始化 SortedList<TKey,TValue> 类的新实例,该实例为空,具有默认的初始容量并使用指定的 IComparer<T>

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

初始化 SortedList<TKey,TValue> 类的新实例,该实例包含从指定的 IDictionary<TKey,TValue> 中复制的元素,其容量足以容纳所复制的元素数并使用默认的 IComparer<T>

SortedList<TKey,TValue>(Int32)

初始化 SortedList<TKey,TValue> 类的新实例,该示例为空且具有指定的初始容量,并使用默认的 IComparer<T>

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

初始化 SortedList<TKey,TValue> 类的新实例,该实例包含从指定的 IDictionary<TKey,TValue> 中复制的元素,其容量足以容纳所复制的元素数并使用指定的 IComparer<T>

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

初始化 SortedList<TKey,TValue> 类的新实例,该实例为空,具有指定的初始容量并使用指定的 IComparer<T>

SortedList<TKey,TValue>()

Source:
SortedList.cs
Source:
SortedList.cs
Source:
SortedList.cs

初始化 SortedList<TKey,TValue> 类的新实例,该示例为空且具有默认的初始容量,并使用默认的 IComparer<T>

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

示例

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

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

// 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
// Create a new sorted list of strings, with string
// keys.
let openWith = 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");
with
    | :? ArgumentException ->
        printfn "An element with Key = \"txt\" already exists."

注解

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

此构造函数对 的初始容量 SortedList<TKey,TValue>使用默认值。 若要设置初始容量,请使用 SortedList<TKey,TValue>(Int32) 构造函数。 如果可以估计集合的最终大小,则指定初始容量就无需在向 中添加元素 SortedList<TKey,TValue>时执行大量调整大小操作。

此构造函数使用 的默认比较器。TKey 若要指定比较器,请使用 SortedList<TKey,TValue>(IComparer<TKey>) 构造函数。 默认比较器 Comparer<T>.Default 检查键类型 TKey 是否实现 System.IComparable<T> 并使用该实现(如果可用)。 如果没有, Comparer<T>.Default 则检查密钥类型 TKey 是否实现 System.IComparable。 如果键类型 TKey 不实现任一 System.Collections.Generic.IComparer<T> 接口,则可以在接受参数的构造函数重载中指定实现 comparer

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

另请参阅

适用于

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

Source:
SortedList.cs
Source:
SortedList.cs
Source:
SortedList.cs

初始化 SortedList<TKey,TValue> 类的新实例,该实例为空,具有默认的初始容量并使用指定的 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> 实现。

- 或 -

null,则为这类键使用默认的 Comparer<T>

示例

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

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> 的。

此构造函数对 的初始容量 SortedList<TKey,TValue>使用默认值。 若要设置初始容量,请使用 SortedList<TKey,TValue>(Int32, IComparer<TKey>) 构造函数。 如果可以估计集合的最终大小,则指定初始容量就无需在向 中添加元素 SortedList<TKey,TValue>时执行大量调整大小操作。

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

另请参阅

适用于

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

Source:
SortedList.cs
Source:
SortedList.cs
Source:
SortedList.cs

初始化 SortedList<TKey,TValue> 类的新实例,该实例包含从指定的 IDictionary<TKey,TValue> 中复制的元素,其容量足以容纳所复制的元素数并使用默认的 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>

例外

dictionarynull

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

示例

下面的代码示例演示如何使用 SortedList<TKey,TValue> 将 传递给Dictionary<TKey,TValue>SortedList<TKey,TValue>(IDictionary<TKey,TValue>)构造函数,在 中创建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 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 中的每个键也必须根据默认比较器是唯一的。

SortedList<TKey,TValue> 的容量设置为 中的 dictionary元素数,因此在填充列表时不会调整大小。

此构造函数使用 的默认比较器。TKey 若要指定比较器,请使用 SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) 构造函数。 默认比较器 Comparer<T>.Default 检查键类型 TKey 是否实现 System.IComparable<T> 并使用该实现(如果可用)。 如果没有, Comparer<T>.Default 则检查密钥类型 TKey 是否实现 System.IComparable。 如果键类型 TKey 不实现任一 System.Collections.Generic.IComparer<T> 接口,则可以在接受参数的构造函数重载中指定实现 comparer

中的 dictionary 键将复制到新的 SortedList<TKey,TValue> 并排序一次,这使此构造函数成为 O (n 日志 n) 操作。

另请参阅

适用于

SortedList<TKey,TValue>(Int32)

Source:
SortedList.cs
Source:
SortedList.cs
Source:
SortedList.cs

初始化 SortedList<TKey,TValue> 类的新实例,该示例为空且具有指定的初始容量,并使用默认的 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> 可包含的初始元素数。

例外

capacity 小于零。

示例

下面的代码示例创建一个初始容量为 4 的排序列表,并使用 4 个条目填充该列表。

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> 的。

SortedList<TKey,TValue> 容量是 在调整大小之前可以容纳的元素 SortedList<TKey,TValue> 数。 当元素添加到 时 SortedList<TKey,TValue>,容量会根据需要通过重新分配内部数组自动增加。

如果可以估计集合的大小,则指定初始容量就无需在向 中添加元素 SortedList<TKey,TValue>时执行大量调整大小操作。

可以通过调用 TrimExcess 或通过显式设置 Capacity 属性来降低容量。 降低容量会重新分配内存并复制 中的所有 SortedList<TKey,TValue>元素。

此构造函数使用 的默认比较器。TKey 若要指定比较器,请使用 SortedList<TKey,TValue>(Int32, IComparer<TKey>) 构造函数。 默认比较器 Comparer<T>.Default 检查键类型 TKey 是否实现 System.IComparable<T> 并使用该实现(如果可用)。 如果没有, Comparer<T>.Default 则检查密钥类型 TKey 是否实现 System.IComparable。 如果键类型 TKey 不实现任一 System.Collections.Generic.IComparer<T> 接口,则可以在接受参数的构造函数重载中指定实现 comparer

此构造函数是 O (n) 操作,其中 ncapacity

另请参阅

适用于

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

Source:
SortedList.cs
Source:
SortedList.cs
Source:
SortedList.cs

初始化 SortedList<TKey,TValue> 类的新实例,该实例包含从指定的 IDictionary<TKey,TValue> 中复制的元素,其容量足以容纳所复制的元素数并使用指定的 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>

comparer
IComparer<TKey>

在对键进行比较时使用的 IComparer<T> 实现。

- 或 -

null,则为这类键使用默认的 Comparer<T>

例外

dictionarynull

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

示例

下面的代码示例演示如何通过将 SortedList<TKey,TValue> 传递给 Dictionary<TKey,TValue>SortedList<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");

        // 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 中的每个键也必须根据指定的比较器是唯一的。

SortedList<TKey,TValue> 的容量设置为 中的 dictionary元素数,因此在填充列表时不会调整大小。

中的 dictionary 键将复制到新的 SortedList<TKey,TValue> 并排序一次,这使此构造函数成为 O (n 日志 n) 操作。

另请参阅

适用于

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

Source:
SortedList.cs
Source:
SortedList.cs
Source:
SortedList.cs

初始化 SortedList<TKey,TValue> 类的新实例,该实例为空,具有指定的初始容量并使用指定的 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> 可包含的初始元素数。

comparer
IComparer<TKey>

在对键进行比较时使用的 IComparer<T> 实现。

- 或 -

null,则为这类键使用默认的 Comparer<T>

例外

capacity 小于零。

示例

下面的代码示例创建一个初始容量为 5 的排序列表,并为当前区域性创建不区分大小写的比较器。 该示例添加四个元素,一些元素使用小写键,一些元素使用大写键。 然后,该示例尝试添加一个元素,该元素的键仅在大小写上与现有键不同,捕获生成的异常,并显示错误消息。 最后,该示例按不区分大小写的排序顺序显示元素。

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> 的。

SortedList<TKey,TValue> 容量是 在调整大小之前可以容纳的元素 SortedList<TKey,TValue> 数。 当元素添加到 时 SortedList<TKey,TValue>,容量会根据需要通过重新分配内部数组自动增加。

如果可以估计集合的大小,则指定初始容量就无需在向 中添加元素 SortedList<TKey,TValue>时执行大量调整大小操作。

可以通过调用 TrimExcess 或通过显式设置 Capacity 属性来降低容量。 降低容量会重新分配内存并复制 中的所有 SortedList<TKey,TValue>元素。

此构造函数是 O (n) 操作,其中 ncapacity

另请参阅

适用于