SortedDictionary<TKey,TValue> 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 SortedDictionary<TKey,TValue> 類別的新執行個體。
多載
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<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
備註
中的每個索引鍵都必須根據預設比較子是唯一 SortedDictionary<TKey,TValue> 的。
SortedDictionary<TKey,TValue> 需要比較子實作才能執行索引鍵比較。 這個建構函式會使用預設的泛型相等比較子 Comparer<T>.Default。 如果類型 TKey
實作 System.IComparable<T> 泛型介面,則預設比較子會使用該實作。 或者,您可以使用接受comparer
參數的IComparer<T>建構函式來指定泛型介面的實作。
此建構函式是 O (1) 作業。
另請參閱
適用於
SortedDictionary<TKey,TValue>(IComparer<TKey>)
初始化 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
備註
中的每個 SortedDictionary<TKey,TValue> 索引鍵都必須根據指定的比較子是唯一的。
SortedDictionary<TKey,TValue> 需要比較子實作才能執行索引鍵比較。 如果 comparer
為null
,這個建構函式會使用預設的泛型相等比較子 。 Comparer<T>.Default 如果類型 TKey
實作 System.IComparable<T> 泛型介面,則預設比較子會使用該實作。
此建構函式是 O (1) 作業。
另請參閱
適用於
SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>)
初始化 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
- IDictionary<TKey,TValue>
要將其項目複製到新 IDictionary<TKey,TValue> 的 SortedDictionary<TKey,TValue>。
例外狀況
dictionary
為 null
。
dictionary
包含一或多個重複的索引鍵。
範例
下列程式代碼範例示範如何使用 SortedDictionary<TKey,TValue> ,將 傳遞Dictionary<TKey,TValue>至 SortedDictionary<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.
//
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
備註
中的每個 SortedDictionary<TKey,TValue> 索引鍵都必須根據預設比較子是唯一的;因此,來源 dictionary
中的每個索引鍵也必須根據預設比較子是唯一的。
SortedDictionary<TKey,TValue> 需要比較子實作才能執行索引鍵比較。 這個建構函式會使用預設的泛型相等比較子 。 Comparer<T>.Default 如果類型 TKey
實作 System.IComparable<T> 泛型介面,則預設比較子會使用該實作。 或者,您可以使用接受comparer
參數的IComparer<T>建構函式來指定泛型介面的實作。
此建構函式是 O (n
記錄 n
) 作業,其中 n
是中的 dictionary
元素數目。
另請參閱
適用於
SortedDictionary<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>)
初始化 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))
參數
- dictionary
- IDictionary<TKey,TValue>
要將其項目複製到新 IDictionary<TKey,TValue> 的 SortedDictionary<TKey,TValue>。
- comparer
- IComparer<TKey>
比較索引鍵時所要使用的 IComparer<T> 實作,或是 null
,表示要為索引鍵的類型使用預設 Comparer<T>。
例外狀況
dictionary
為 null
。
dictionary
包含一或多個重複的索引鍵。
範例
下列程式代碼範例示範如何使用 SortedDictionary<TKey,TValue> ,藉由將 傳遞Dictionary<TKey,TValue>至 SortedDictionary<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");
// 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
備註
中的每個 SortedDictionary<TKey,TValue> 索引鍵都必須根據指定的比較子是唯一的;因此,來源 dictionary
中的每個索引鍵也必須根據指定的比較子是唯一的。
SortedDictionary<TKey,TValue> 需要比較子實作才能執行索引鍵比較。 如果 comparer
為null
,這個建構函式會使用預設的泛型相等比較子 。 Comparer<T>.Default 如果類型 TKey
實作 System.IComparable<T> 泛型介面,則預設比較子會使用該實作。
此建構函式是 O (n
記錄 n
) 作業,其中 n
是中的 dictionary
元素數目。