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 已排序,此函式就是 (的 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 * nOtherwise 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 已排序,此函式就是 (的 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 * nOtherwise 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.

另請參閱

適用於