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

初始化 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

注解

每个键都必须根据默认比较器是唯一 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>)

初始化 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>)

初始化 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 进行排序,则此构造函数是 O (n) 操作,其中 n 元素的数目 dictionary。 否则,它是 O (n*n) 操作。

另请参阅

适用于

SortedList<TKey,TValue>(Int32)

初始化 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> 数。 随着元素添加到 a 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>)

初始化 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 进行排序,则此构造函数是 O (n) 操作,其中 n 元素数 dictionary。 否则,它是 O (n*n) 操作。

另请参阅

适用于

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

初始化 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> 数。 随着元素添加到 a SortedList<TKey,TValue>,通过重新分配内部数组,容量会自动增加。

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

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

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

另请参阅

适用于