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.cs
來源:
SortedList.cs
來源:
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>)

來源:
SortedList.cs
來源:
SortedList.cs
來源:
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>)

來源:
SortedList.cs
來源:
SortedList.cs
來源:
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)

來源:
SortedList.cs
來源:
SortedList.cs
來源:
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>)

來源:
SortedList.cs
來源:
SortedList.cs
來源:
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>)

來源:
SortedList.cs
來源:
SortedList.cs
來源:
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

另請參閱

適用於