SortedList<TKey,TValue> SortedList<TKey,TValue> SortedList<TKey,TValue> SortedList<TKey,TValue> Constructors

定義

多載

SortedList<TKey,TValue>() SortedList<TKey,TValue>() 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<TKey>) SortedList<TKey,TValue>(IComparer<TKey>) 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>) SortedList<TKey,TValue>(IDictionary<TKey,TValue>) 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>(Int32) SortedList<TKey,TValue>(Int32) 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<TKey>) SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) 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>(Int32, IComparer<TKey>) SortedList<TKey,TValue>(Int32, IComparer<TKey>) 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>() 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>.

此函式會使用的預設TKey比較子。This 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子會檢查金鑰System.IComparable<T>類型TKey是否會執行, 並使用該實值 (如果有的話)。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.IComparable類型是否已執行。If not, Comparer<T>.Default checks whether the key type TKey implements System.IComparable. 如果索引鍵類型TKey未執行任一介面, 您可以在接受comparer參數System.Collections.Generic.IComparer<T>的函式多載中指定實作為執行。If 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<TKey>) SortedList<TKey,TValue>(IComparer<TKey>) 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);
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
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>) SortedList<TKey,TValue>(IDictionary<TKey,TValue>) 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 包含一個或多個重複的索引鍵。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
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.

此函式會使用的預設TKey比較子。This 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子會檢查金鑰System.IComparable<T>類型TKey是否會執行, 並使用該實值 (如果有的話)。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.IComparable類型是否已執行。If not, Comparer<T>.Default checks whether the key type TKey implements System.IComparable. 如果索引鍵類型TKey未執行任一介面, 您可以在接受comparer參數System.Collections.Generic.IComparer<T>的函式多載中指定實作為執行。If 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的資料已排序, 則此函式是 On() 運算, n其中是中dictionary的專案數。If 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>(Int32) SortedList<TKey,TValue>(Int32) 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 Int32 Int32 Int32

SortedList<TKey,TValue> 可包含的初始項目數。The initial number of elements that the SortedList<TKey,TValue> can contain.

例外狀況

範例

下列程式碼範例會建立一個已排序的清單, 其初始容量為 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
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或明確Capacity設定屬性來降低容量。The 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>.

此函式會使用的預設TKey比較子。This 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子會檢查金鑰System.IComparable<T>類型TKey是否會執行, 並使用該實值 (如果有的話)。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.IComparable類型是否已執行。If not, Comparer<T>.Default checks whether the key type TKey implements System.IComparable. 如果索引鍵類型TKey未執行任一介面, 您可以在接受comparer參數System.Collections.Generic.IComparer<T>的函式多載中指定實作為執行。If 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<TKey>) SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) 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);
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))

參數

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.

例外狀況

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
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的資料已排序, 則此函式是 On() 運算, n其中是中dictionary的專案數。If 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>(Int32, IComparer<TKey>) SortedList<TKey,TValue>(Int32, IComparer<TKey>) 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);
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 Int32 Int32 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.

例外狀況

範例

下列程式碼範例會建立排序清單, 其中的初始容量為 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
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或明確Capacity設定屬性來降低容量。The 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.

另請參閱

適用於