語言

SortedDictionary<TKey,TValue> 建構函式

定義

初始化 SortedDictionary<TKey,TValue> 類別的新執行個體。

多載

名稱 Description
SortedDictionary<TKey,TValue>()

初始化一個空的類別新實例 SortedDictionary<TKey,TValue> ,並使用該鍵型別的預設 IComparer<T> 實作。

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

初始化一個空的類別新實例 SortedDictionary<TKey,TValue> ,並使用指定的 IComparer<T> 實作來比較鍵數。

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

初始化一個包含從指定SortedDictionary<TKey,TValue>元素複製的類別的新實例IDictionary<TKey,TValue>,並使用該金鑰型別的預設IComparer<T>實作。

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

初始化一個包含從指定SortedDictionary<TKey,TValue>物件複製元素的新類別實例IDictionary<TKey,TValue>,並使用指定的IComparer<T>實作來比較鍵數。

SortedDictionary<TKey,TValue>()

來源:
SortedDictionary.cs
來源:
SortedDictionary.cs
來源:
SortedDictionary.cs
來源:
SortedDictionary.cs
來源:
SortedDictionary.cs

初始化一個空的類別新實例 SortedDictionary<TKey,TValue> ,並使用該鍵型別的預設 IComparer<T> 實作。

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

範例

以下程式碼範例建立一個包含字串鍵的空 SortedDictionary<TKey,TValue> 字串,並使用此 Add 方法加入一些元素。 範例顯示該Add方法在嘗試新增重複鍵時會拋出 。ArgumentException

此程式碼範例是本類別更大範例 SortedDictionary<TKey,TValue> 的一部分。

// Create a new sorted dictionary of strings, with string
// keys.
SortedDictionary<string, string> openWith =
    new SortedDictionary<string, string>();

// Add some elements to the dictionary. 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 dictionary.
try
{
    openWith.Add("txt", "winword.exe");
}
catch (ArgumentException)
{
    Console.WriteLine("An element with Key = \"txt\" already exists.");
}
' Create a new sorted dictionary of strings, with string 
' keys. 
Dim openWith As New SortedDictionary(Of String, String)

' Add some elements to the dictionary. 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 dictionary.
Try
    openWith.Add("txt", "winword.exe")
Catch 
    Console.WriteLine("An element with Key = ""txt"" already exists.")
End Try

備註

根據預設比較器,a SortedDictionary<TKey,TValue> 中的每個金鑰必須是唯一的。

SortedDictionary<TKey,TValue> 需要比較器實作來執行金鑰比較。 此建構器使用預設的通用等號比較器 Comparer<T>.Default。 若型別 TKey 實 System.IComparable<T> 作通用介面,預設比較器則使用該實作。 或者,你也可以使用接受IComparer<T>參數的建構子來指定通用介面的實作comparer。

此構造子為 O(1) 運算。

另請參閱

適用於

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

來源:
SortedDictionary.cs
來源:
SortedDictionary.cs
來源:
SortedDictionary.cs
來源:
SortedDictionary.cs
來源:
SortedDictionary.cs

初始化一個空的類別新實例 SortedDictionary<TKey,TValue> ,並使用指定的 IComparer<T> 實作來比較鍵數。

public:
 SortedDictionary(System::Collections::Generic::IComparer<TKey> ^ comparer);
public SortedDictionary(System.Collections.Generic.IComparer<TKey> comparer);
public SortedDictionary(System.Collections.Generic.IComparer<TKey>? comparer);
new System.Collections.Generic.SortedDictionary<'Key, 'Value> : System.Collections.Generic.IComparer<'Key> -> System.Collections.Generic.SortedDictionary<'Key, 'Value>
Public Sub New (comparer As IComparer(Of TKey))

參數

comparer
IComparer<TKey>

比較金鑰時使用的實作, IComparer<T> 或 null 是使用金 Comparer<T> 鑰類型預設的實作。

範例

以下程式碼範例為當前文化建立 SortedDictionary<TKey,TValue> 一個大小寫不區分比較器的 。 範例新增了四個元素,有些是小寫鍵,有些是大寫鍵。 接著範例嘗試加入一個元素,其鍵值僅在案例上不同,捕捉該異常,並顯示錯誤訊息。 最後,範例以大小寫不區分排序順序顯示元素。

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new SortedDictionary of strings, with string keys
        // and a case-insensitive comparer for the current culture.
        SortedDictionary<string, string> openWith =
                      new SortedDictionary<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");

        // 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 dictionary.");
        }

        // List the contents of the sorted dictionary.
        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 dictionary.

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 SortedDictionary of strings, with string keys 
        ' and a case-insensitive comparer for the current culture.
        Dim openWith As New SortedDictionary(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")

        ' 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 dictionary.")
        End Try
        
        ' List the contents of the sorted dictionary.
        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 dictionary.
'
'Key = bmp, Value = paint.exe
'Key = DIB, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe

備註

根據指定的比較器,a SortedDictionary<TKey,TValue> 中的每個鍵都必須是唯一的。

SortedDictionary<TKey,TValue> 需要比較器實作來執行金鑰比較。 若 comparer , null則此構造子使用預設的通用等號比較器 Comparer<T>.Default。 若型別 TKey 實 System.IComparable<T> 作通用介面,預設比較器則使用該實作。

此構造子為 O(1) 運算。

另請參閱

適用於

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

來源:
SortedDictionary.cs
來源:
SortedDictionary.cs
來源:
SortedDictionary.cs
來源:
SortedDictionary.cs
來源:
SortedDictionary.cs

初始化一個包含從指定SortedDictionary<TKey,TValue>元素複製的類別的新實例IDictionary<TKey,TValue>,並使用該金鑰型別的預設IComparer<T>實作。

public:
 SortedDictionary(System::Collections::Generic::IDictionary<TKey, TValue> ^ dictionary);
public SortedDictionary(System.Collections.Generic.IDictionary<TKey,TValue> dictionary);
new System.Collections.Generic.SortedDictionary<'Key, 'Value> : System.Collections.Generic.IDictionary<'Key, 'Value> -> System.Collections.Generic.SortedDictionary<'Key, 'Value>
Public Sub New (dictionary As IDictionary(Of TKey, TValue))

參數

例外狀況

dictionary 是 null。

dictionary 包含一個或多個重複鍵。

範例

以下程式碼範例展示了如何透過SortedDictionary<TKey,TValue>將 傳遞Dictionary<TKey,TValue>給Dictionary<TKey,TValue>建構子,來建立 中資訊的排序副本SortedDictionary<TKey,TValue>(IComparer<TKey>)。

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 SortedDictionary of strings with string keys,
        // and initialize it with the contents of the Dictionary.
        SortedDictionary<string, string> copy =
                  new SortedDictionary<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 SortedDictionary of strings with string keys, 
        ' and initialize it with the contents of the Dictionary.
        Dim copy As New SortedDictionary(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

備註

A 中的 SortedDictionary<TKey,TValue> 每個金鑰必須根據預設比較器唯一;因此,來源 dictionary 中的每個金鑰也必須根據預設比較器唯一。

SortedDictionary<TKey,TValue> 需要比較器實作來執行金鑰比較。 此構造器使用預設的通用等號比較器 Comparer<T>.Default。 若型別 TKey 實 System.IComparable<T> 作通用介面,預設比較器則使用該實作。 或者,你也可以使用接受IComparer<T>參數的建構子來指定通用介面的實作comparer。

此構造子是一個 O(n log n) 運算,其中 n 是 中的 dictionary元素數。

另請參閱

適用於

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

來源:
SortedDictionary.cs
來源:
SortedDictionary.cs
來源:
SortedDictionary.cs
來源:
SortedDictionary.cs
來源:
SortedDictionary.cs

初始化一個包含從指定SortedDictionary<TKey,TValue>物件複製元素的新類別實例IDictionary<TKey,TValue>,並使用指定的IComparer<T>實作來比較鍵數。

public:
 SortedDictionary(System::Collections::Generic::IDictionary<TKey, TValue> ^ dictionary, System::Collections::Generic::IComparer<TKey> ^ comparer);
public SortedDictionary(System.Collections.Generic.IDictionary<TKey,TValue> dictionary, System.Collections.Generic.IComparer<TKey> comparer);
public SortedDictionary(System.Collections.Generic.IDictionary<TKey,TValue> dictionary, System.Collections.Generic.IComparer<TKey>? comparer);
new System.Collections.Generic.SortedDictionary<'Key, 'Value> : System.Collections.Generic.IDictionary<'Key, 'Value> * System.Collections.Generic.IComparer<'Key> -> System.Collections.Generic.SortedDictionary<'Key, 'Value>
Public Sub New (dictionary As IDictionary(Of TKey, TValue), comparer As IComparer(Of TKey))

參數

comparer
IComparer<TKey>

比較金鑰時使用的實作, IComparer<T> 或 null 是使用金 Comparer<T> 鑰類型預設的實作。

例外狀況

dictionary 是 null。

dictionary 包含一個或多個重複鍵。

範例

以下程式碼範例展示了如何透過SortedDictionary<TKey,TValue>將 傳遞Dictionary<TKey,TValue>給Dictionary<TKey,TValue>建構子,來建立大小寫不區分的排序副本SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>)。 在此範例中,大小寫不區分的比較器是針對當前文化。

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");

        // List the contents of the Dictionary.
        Console.WriteLine();
        foreach( KeyValuePair<string, string> kvp in openWith)
        {
            Console.WriteLine("Key = {0}, Value = {1}", kvp.Key,
                kvp.Value);
        }

        // Create a SortedDictionary of strings with string keys and a
        // case-insensitive equality comparer for the current culture,
        // and initialize it with the contents of the Dictionary.
        SortedDictionary<string, string> copy =
                    new SortedDictionary<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 = txt, Value = notepad.exe
Key = Bmp, Value = paint.exe
Key = DIB, Value = paint.exe
Key = rtf, Value = wordpad.exe

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")
        
        ' List the contents of the Dictionary.
        Console.WriteLine()
        For Each kvp As KeyValuePair(Of String, String) In openWith
            Console.WriteLine("Key = {0}, Value = {1}", _
                kvp.Key, kvp.Value)
        Next kvp

        ' Create a SortedDictionary 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 SortedDictionary(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 = txt, Value = notepad.exe
'Key = Bmp, Value = paint.exe
'Key = DIB, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'
'Key = Bmp, Value = paint.exe
'Key = DIB, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe

備註

A 中的 SortedDictionary<TKey,TValue> 每個金鑰必須根據指定的比較器唯一;因此,來源 dictionary 中的每個金鑰也必須根據指定的比較器唯一。

SortedDictionary<TKey,TValue> 需要比較器實作來執行金鑰比較。 若 comparer , null則此構造子使用預設的通用等號比較器 Comparer<T>.Default。 若型別 TKey 實 System.IComparable<T> 作通用介面,預設比較器則使用該實作。

此構造子是一個 O(n log n) 運算,其中 n 是 中的 dictionary元素數。

另請參閱

適用於