共用方式為


Dictionary<TKey,TValue> 建構函式

定義

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

多載

Dictionary<TKey,TValue>()

初始化 Dictionary<TKey,TValue> 類別的新執行個體,這個執行個體是空白的、具有預設的初始容量,並使用索引鍵類型的預設相等比較子。

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

初始化 Dictionary<TKey,TValue> 類別的新執行個體,其中包含從指定的 IDictionary<TKey,TValue> 複製的項目,並使用索引鍵類型的預設相等比較子。

Dictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>)

初始化 Dictionary<TKey,TValue> 類別的新執行個體,這個類別包含從指定的 IEnumerable<T> 所複製項目。

Dictionary<TKey,TValue>(IEqualityComparer<TKey>)

初始化 Dictionary<TKey,TValue> 類別的新執行個體,這個執行個體是空白的、具有預設的初始容量,並使用指定的 IEqualityComparer<T>

Dictionary<TKey,TValue>(Int32)

初始化 Dictionary<TKey,TValue> 類別的新執行個體,這個執行個體是空白的、具有指定的初始容量,並使用索引鍵類型的預設相等比較子。

Dictionary<TKey,TValue>(IDictionary<TKey,TValue>, IEqualityComparer<TKey>)

初始化 Dictionary<TKey,TValue> 類別的新執行個體,其中包含從指定的 IDictionary<TKey,TValue> 複製的項目,並使用指定的 IEqualityComparer<T>

Dictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>, IEqualityComparer<TKey>)

初始化 Dictionary<TKey,TValue> 類別的新執行個體,其中包含從指定的 IEnumerable<T> 複製的項目,並使用指定的 IEqualityComparer<T>

Dictionary<TKey,TValue>(Int32, IEqualityComparer<TKey>)

初始化 Dictionary<TKey,TValue> 類別的新執行個體,這個執行個體是空白的、具有指定的初始容量,並使用指定的 IEqualityComparer<T>

Dictionary<TKey,TValue>(SerializationInfo, StreamingContext)
已淘汰.

使用序列化資料,初始化 Dictionary<TKey,TValue> 類別的新執行個體。

Dictionary<TKey,TValue>()

來源:
Dictionary.cs
來源:
Dictionary.cs
來源:
Dictionary.cs

初始化 Dictionary<TKey,TValue> 類別的新執行個體,這個執行個體是空白的、具有預設的初始容量,並使用索引鍵類型的預設相等比較子。

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

範例

下列程式代碼範例會使用字串索引鍵建立空 Dictionary<TKey,TValue> 字串,並使用 Add 方法來加入某些元素。 此範例示範 Add 嘗試加入重複索引鍵時,方法會擲回 ArgumentException

此程式代碼範例是提供給 類別之較大範例的 Dictionary<TKey,TValue> 一部分。

// Create a new dictionary of strings, with string keys.
//
Dictionary<String^, String^>^ openWith =
    gcnew Dictionary<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 dictionary of strings, with string keys.
//
Dictionary<string, string> openWith =
    new Dictionary<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 dictionary of strings, with string keys.
'
Dim openWith As New Dictionary(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

備註

中的每個 Dictionary<TKey,TValue> 索引鍵都必須根據預設相等比較子是唯一的。

Dictionary<TKey,TValue> 需要相等實作,以判斷索引鍵是否相等。 此建構函式會使用預設泛型相等比較子 。 EqualityComparer<T>.Default 如果類型 TKey 實作 System.IEquatable<T> 泛型介面,則預設相等比較子會使用該實作。 或者,您可以使用接受參數的 IEqualityComparer<T> 建構函式來指定泛型介面的實 comparer 作。

注意

如果您可以估計集合的大小,使用指定初始容量的建構函式,就不需要在將專案加入 至 Dictionary<TKey,TValue>時執行一些重設大小作業。

此建構函式是 O (1) 作業。

另請參閱

適用於

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

來源:
Dictionary.cs
來源:
Dictionary.cs
來源:
Dictionary.cs

初始化 Dictionary<TKey,TValue> 類別的新執行個體,其中包含從指定的 IDictionary<TKey,TValue> 複製的項目,並使用索引鍵類型的預設相等比較子。

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

參數

dictionary
IDictionary<TKey,TValue>

要將其項目複製到新 IDictionary<TKey,TValue>Dictionary<TKey,TValue>

例外狀況

dictionarynull

dictionary 包含一或多個重複的索引鍵。

範例

下列程式代碼範例示範如何使用 建 Dictionary<TKey,TValue>(IEqualityComparer<TKey>) 構函式,以另一個字典中排序的內容初始化 Dictionary<TKey,TValue> 。 程式代碼範例會建立 , SortedDictionary<TKey,TValue> 並以隨機順序填入數據,然後將 傳遞給 SortedDictionary<TKey,TValue>Dictionary<TKey,TValue>(IEqualityComparer<TKey>) 構函式,建立 Dictionary<TKey,TValue> 排序的 。 如果您需要建置在某個時間點變成靜態的已排序字典,這會很有用;將數據從 SortedDictionary<TKey,TValue> 複製到 可 Dictionary<TKey,TValue> 改善擷取速度。

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new sorted dictionary of strings, with string
        // keys.
        SortedDictionary<string, string> openWith =
            new SortedDictionary<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 Dictionary of strings with string keys, and
        // initialize it with the contents of the sorted dictionary.
        Dictionary<string, string> copy =
            new Dictionary<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 sorted dictionary of strings, with string 
        ' keys.
        Dim openWith As New SortedDictionary(Of String, String)
        
        ' Add some elements to the sorted dictionary. 
        openWith.Add("txt", "notepad.exe")
        openWith.Add("bmp", "paint.exe")
        openWith.Add("dib", "paint.exe")
        openWith.Add("rtf", "wordpad.exe")
        
        ' Create a Dictionary of strings with string keys, and 
        ' initialize it with the contents of the sorted dictionary.
        Dim copy As New Dictionary(Of String, String)(openWith)

        ' List the 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

備註

中的每個 Dictionary<TKey,TValue> 索引鍵都必須根據預設相等比較子是唯一的;同樣地,來源 dictionary 中的每個索引鍵也必須根據預設相等比較子是唯一的。

Dictionary<TKey,TValue> 的初始容量夠大,足以包含 中的所有 dictionary元素。

Dictionary<TKey,TValue> 需要相等實作,以判斷索引鍵是否相等。 此建構函式會使用預設泛型相等比較子 。 EqualityComparer<T>.Default 如果類型 TKey 實作 System.IEquatable<T> 泛型介面,則預設相等比較子會使用該實作。 或者,您可以使用接受參數的 IEqualityComparer<T> 建構函式來指定泛型介面的實 comparer 作。

此建構函式是 o (n) 作業,其中 n 是 中的 dictionary項目數目。

另請參閱

適用於

Dictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>)

來源:
Dictionary.cs
來源:
Dictionary.cs
來源:
Dictionary.cs

初始化 Dictionary<TKey,TValue> 類別的新執行個體,這個類別包含從指定的 IEnumerable<T> 所複製項目。

public:
 Dictionary(System::Collections::Generic::IEnumerable<System::Collections::Generic::KeyValuePair<TKey, TValue>> ^ collection);
public Dictionary (System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>> collection);
new System.Collections.Generic.Dictionary<'Key, 'Value> : seq<System.Collections.Generic.KeyValuePair<'Key, 'Value>> -> System.Collections.Generic.Dictionary<'Key, 'Value>
Public Sub New (collection As IEnumerable(Of KeyValuePair(Of TKey, TValue)))

參數

collection
IEnumerable<KeyValuePair<TKey,TValue>>

要將其項目複製到新 IEnumerable<T>Dictionary<TKey,TValue>

例外狀況

collectionnull

collection 包含一或多個重複的索引鍵。

適用於

Dictionary<TKey,TValue>(IEqualityComparer<TKey>)

來源:
Dictionary.cs
來源:
Dictionary.cs
來源:
Dictionary.cs

初始化 Dictionary<TKey,TValue> 類別的新執行個體,這個執行個體是空白的、具有預設的初始容量,並使用指定的 IEqualityComparer<T>

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

參數

comparer
IEqualityComparer<TKey>

比較索引鍵時所要使用的 IEqualityComparer<T> 實作,或是 null,表示要為索引鍵的類型使用預設 EqualityComparer<T>

範例

下列程式代碼範例會 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 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");

        // 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 = txt, Value = notepad.exe
Key = bmp, Value = paint.exe
Key = DIB, Value = paint.exe
Key = rtf, Value = wordpad.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 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")

        ' 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 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 = txt, Value = notepad.exe
'Key = bmp, Value = paint.exe
'Key = DIB, Value = paint.exe
'Key = rtf, Value = wordpad.exe

備註

使用此建構函式搭配 類別所提供的 StringComparer 不區分大小寫字串比較子,以建立不區分大小寫字串索引鍵的字典。

中的每個 Dictionary<TKey,TValue> 索引鍵都必須根據指定的比較子是唯一的。

Dictionary<TKey,TValue> 需要相等實作,以判斷索引鍵是否相等。 如果為 comparernull,這個建構函式會使用預設泛型相等比較子 EqualityComparer<T>.Default 如果類型 TKey 實作 System.IEquatable<T> 泛型介面,則預設相等比較子會使用該實作。

注意

如果您可以估計集合的大小,使用指定初始容量的建構函式,就不需要在將專案加入 至 Dictionary<TKey,TValue>時執行一些重設大小作業。

此建構函式是 O (1) 作業。

另請參閱

適用於

Dictionary<TKey,TValue>(Int32)

來源:
Dictionary.cs
來源:
Dictionary.cs
來源:
Dictionary.cs

初始化 Dictionary<TKey,TValue> 類別的新執行個體,這個執行個體是空白的、具有指定的初始容量,並使用索引鍵類型的預設相等比較子。

public:
 Dictionary(int capacity);
public Dictionary (int capacity);
new System.Collections.Generic.Dictionary<'Key, 'Value> : int -> System.Collections.Generic.Dictionary<'Key, 'Value>
Public Sub New (capacity As Integer)

參數

capacity
Int32

Dictionary<TKey,TValue> 可包含的初始項目數。

例外狀況

capacity 小於 0。

範例

下列程式代碼範例會建立初始容量為 4 的字典,並填入 4 個專案。

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new dictionary of strings, with string keys and
        // an initial capacity of 4.
        Dictionary<string, string> openWith =
                               new Dictionary<string, string>(4);

        // Add 4 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);
        }
    }
}

/* 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
 */
Imports System.Collections.Generic

Public Class Example
    
    Public Shared Sub Main() 

        ' Create a new dictionary of strings, with string keys and
        ' an initial capacity of 4.
        Dim openWith As New Dictionary(Of String, String)(4)
        
        ' Add 4 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

    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

備註

中的每個 Dictionary<TKey,TValue> 索引鍵都必須根據預設相等比較子是唯一的。

Dictionary<TKey,TValue> 容量是必要重設大小之前可以新增至 Dictionary<TKey,TValue> 的項目數目。 當元素新增至 Dictionary<TKey,TValue>時,重新配置內部數位會自動增加容量。

如果可以估計集合的大小,則指定初始容量就不需要在將元素加入 至 Dictionary<TKey,TValue>時執行一些重設大小作業。

Dictionary<TKey,TValue> 需要相等實作,以判斷索引鍵是否相等。 此建構函式會使用預設泛型相等比較子 。 EqualityComparer<T>.Default 如果類型 TKey 實作 System.IEquatable<T> 泛型介面,則預設相等比較子會使用該實作。 或者,您可以使用接受參數的 IEqualityComparer<T> 建構函式來指定泛型介面的實 comparer 作。

此建構函式是 O (1) 作業。

另請參閱

適用於

Dictionary<TKey,TValue>(IDictionary<TKey,TValue>, IEqualityComparer<TKey>)

來源:
Dictionary.cs
來源:
Dictionary.cs
來源:
Dictionary.cs

初始化 Dictionary<TKey,TValue> 類別的新執行個體,其中包含從指定的 IDictionary<TKey,TValue> 複製的項目,並使用指定的 IEqualityComparer<T>

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

參數

dictionary
IDictionary<TKey,TValue>

要將其項目複製到新 IDictionary<TKey,TValue>Dictionary<TKey,TValue>

comparer
IEqualityComparer<TKey>

比較索引鍵時所要使用的 IEqualityComparer<T> 實作,或是 null,表示要為索引鍵的類型使用預設 EqualityComparer<T>

例外狀況

dictionarynull

dictionary 包含一或多個重複的索引鍵。

範例

下列程式代碼範例示範如何使用 建 Dictionary<TKey,TValue>(IDictionary<TKey,TValue>, IEqualityComparer<TKey>) 構函式,以不區分大小寫的排序內容,從另一個字典初始化 Dictionary<TKey,TValue> 。 程式代碼範例會 SortedDictionary<TKey,TValue> 使用不區分大小寫的比較子建立 ,並以隨機順序填入數據,然後將 傳遞 SortedDictionary<TKey,TValue>Dictionary<TKey,TValue>(IDictionary<TKey,TValue>, IEqualityComparer<TKey>) 建構函式,以及不區分大小寫的相等比較子,建立 Dictionary<TKey,TValue> 排序的 。 如果您需要建置在某個時間點變成靜態的已排序字典,這會很有用;將數據從 SortedDictionary<TKey,TValue> 複製到 可 Dictionary<TKey,TValue> 改善擷取速度。

注意

當您建立具有不區分大小寫比較子的新字典,並使用區分大小寫比較子的字典中的專案填入該字典時,如本範例所示,如果輸入字典的索引鍵只有大小寫不同,就會發生例外狀況。

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new sorted dictionary of strings, with string
        // keys and a case-insensitive comparer.
        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");

        // Create a Dictionary of strings with string keys and a
        // case-insensitive equality comparer, and initialize it
        // with the contents of the sorted dictionary.
        Dictionary<string, string> copy =
                new Dictionary<string, string>(openWith,
                    StringComparer.CurrentCultureIgnoreCase);

        // 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 sorted dictionary of strings, with string 
        ' keys and a case-insensitive comparer.
        Dim openWith As New SortedDictionary(Of String, String)( _
            StringComparer.CurrentCultureIgnoreCase)
        
        ' Add some elements to the sorted dictionary. 
        openWith.Add("txt", "notepad.exe")
        openWith.Add("Bmp", "paint.exe")
        openWith.Add("DIB", "paint.exe")
        openWith.Add("rtf", "wordpad.exe")
        
        ' Create a Dictionary of strings with string keys and a 
        ' case-insensitive equality comparer, and initialize it
        ' with the contents of the sorted dictionary.
        Dim copy As New Dictionary(Of String, String)(openWith, _
            StringComparer.CurrentCultureIgnoreCase)

        ' List the 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

備註

使用此建構函式搭配 類別所提供的 StringComparer 不區分大小寫字串比較子,以建立不區分大小寫字串索引鍵的字典。

中的每個 Dictionary<TKey,TValue> 索引鍵都必須根據指定的比較子是唯一的;同樣地,來源 dictionary 中的每個索引鍵也必須根據指定的比較子是唯一的。

注意

例如,如果 comparer 類別提供的 StringComparer 其中一個不區分大小寫的字串比較子,而且 dictionary 不使用不區分大小寫的比較子索引鍵,就會發生重複索引鍵。

Dictionary<TKey,TValue> 的初始容量夠大,足以包含 中的所有 dictionary元素。

Dictionary<TKey,TValue> 需要相等實作,以判斷索引鍵是否相等。 如果為 comparernull,這個建構函式會使用預設泛型相等比較子 EqualityComparer<T>.Default 如果類型 TKey 實作 System.IEquatable<T> 泛型介面,則預設相等比較子會使用該實作。

此建構函式是 O (n) 作業,其中 n 是中的 dictionary項目數目。

另請參閱

適用於

Dictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>, IEqualityComparer<TKey>)

來源:
Dictionary.cs
來源:
Dictionary.cs
來源:
Dictionary.cs

初始化 Dictionary<TKey,TValue> 類別的新執行個體,其中包含從指定的 IEnumerable<T> 複製的項目,並使用指定的 IEqualityComparer<T>

public:
 Dictionary(System::Collections::Generic::IEnumerable<System::Collections::Generic::KeyValuePair<TKey, TValue>> ^ collection, System::Collections::Generic::IEqualityComparer<TKey> ^ comparer);
public Dictionary (System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>> collection, System.Collections.Generic.IEqualityComparer<TKey>? comparer);
public Dictionary (System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>> collection, System.Collections.Generic.IEqualityComparer<TKey> comparer);
new System.Collections.Generic.Dictionary<'Key, 'Value> : seq<System.Collections.Generic.KeyValuePair<'Key, 'Value>> * System.Collections.Generic.IEqualityComparer<'Key> -> System.Collections.Generic.Dictionary<'Key, 'Value>
Public Sub New (collection As IEnumerable(Of KeyValuePair(Of TKey, TValue)), comparer As IEqualityComparer(Of TKey))

參數

collection
IEnumerable<KeyValuePair<TKey,TValue>>

要將其項目複製到新 IEnumerable<T>Dictionary<TKey,TValue>

comparer
IEqualityComparer<TKey>

比較索引鍵時所要使用的 IEqualityComparer<T> 實作,或是 null,表示要為索引鍵的類型使用預設 EqualityComparer<T>

例外狀況

collectionnull

collection 包含一或多個重複的索引鍵。

適用於

Dictionary<TKey,TValue>(Int32, IEqualityComparer<TKey>)

來源:
Dictionary.cs
來源:
Dictionary.cs
來源:
Dictionary.cs

初始化 Dictionary<TKey,TValue> 類別的新執行個體,這個執行個體是空白的、具有指定的初始容量,並使用指定的 IEqualityComparer<T>

public:
 Dictionary(int capacity, System::Collections::Generic::IEqualityComparer<TKey> ^ comparer);
public Dictionary (int capacity, System.Collections.Generic.IEqualityComparer<TKey> comparer);
public Dictionary (int capacity, System.Collections.Generic.IEqualityComparer<TKey>? comparer);
new System.Collections.Generic.Dictionary<'Key, 'Value> : int * System.Collections.Generic.IEqualityComparer<'Key> -> System.Collections.Generic.Dictionary<'Key, 'Value>
Public Sub New (capacity As Integer, comparer As IEqualityComparer(Of TKey))

參數

capacity
Int32

Dictionary<TKey,TValue> 可包含的初始項目數。

comparer
IEqualityComparer<TKey>

比較索引鍵時所要使用的 IEqualityComparer<T> 實作,或是 null,表示要為索引鍵的類型使用預設 EqualityComparer<T>

例外狀況

capacity 小於 0。

範例

下列程式代碼範例會 Dictionary<TKey,TValue> 建立初始容量為5的,以及目前文化特性不區分大小寫的相等比較子。 此範例會新增四個元素,其中有些具有小寫索引鍵,有些則使用大寫索引鍵。 然後,此範例會嘗試新增索引鍵與現有索引鍵不同的元素,只依大小寫、攔截產生的例外狀況,並顯示錯誤訊息。 最後,此範例會顯示字典中的專案。

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new dictionary of strings, with string keys, an
        // initial capacity of 5, and a case-insensitive equality
        // comparer.
        Dictionary<string, string> openWith =
                      new Dictionary<string, string>(5,
                          StringComparer.CurrentCultureIgnoreCase);

        // Add 4 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 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 = txt, Value = notepad.exe
Key = bmp, Value = paint.exe
Key = DIB, Value = paint.exe
Key = rtf, Value = wordpad.exe
 */
Imports System.Collections.Generic

Public Class Example
    
    Public Shared Sub Main() 

        ' Create a new Dictionary of strings, with string keys, an
        ' initial capacity of 5, and a case-insensitive equality
        ' comparer.
        Dim openWith As New Dictionary(Of String, String)(5, _
            StringComparer.CurrentCultureIgnoreCase)
        
        ' Add 4 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 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 = txt, Value = notepad.exe
'Key = bmp, Value = paint.exe
'Key = DIB, Value = paint.exe
'Key = rtf, Value = wordpad.exe

備註

使用此建構函式搭配 類別所提供的 StringComparer 不區分大小寫字串比較子,以建立不區分大小寫字串索引鍵的字典。

中的每個 Dictionary<TKey,TValue> 索引鍵都必須根據指定的比較子是唯一的。

Dictionary<TKey,TValue> 容量是必要重設大小之前可以新增至 Dictionary<TKey,TValue> 的項目數目。 當元素新增至 Dictionary<TKey,TValue>時,重新配置內部數位會自動增加容量。

如果可以估計集合的大小,則指定初始容量就不需要在將元素加入 至 Dictionary<TKey,TValue>時執行一些重設大小作業。

Dictionary<TKey,TValue> 需要相等實作,以判斷索引鍵是否相等。 如果為 comparernull,這個建構函式會使用預設泛型相等比較子 EqualityComparer<T>.Default 如果類型 TKey 實作 System.IEquatable<T> 泛型介面,則預設相等比較子會使用該實作。

此建構函式是 O (1) 作業。

另請參閱

適用於

Dictionary<TKey,TValue>(SerializationInfo, StreamingContext)

來源:
Dictionary.cs
來源:
Dictionary.cs
來源:
Dictionary.cs

警告

This API supports obsolete formatter-based serialization. It should not be called or extended by application code.

使用序列化資料,初始化 Dictionary<TKey,TValue> 類別的新執行個體。

protected:
 Dictionary(System::Runtime::Serialization::SerializationInfo ^ info, System::Runtime::Serialization::StreamingContext context);
protected Dictionary (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
[System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
protected Dictionary (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
new System.Collections.Generic.Dictionary<'Key, 'Value> : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Collections.Generic.Dictionary<'Key, 'Value>
[<System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
new System.Collections.Generic.Dictionary<'Key, 'Value> : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Collections.Generic.Dictionary<'Key, 'Value>
Protected Sub New (info As SerializationInfo, context As StreamingContext)

參數

info
SerializationInfo

SerializationInfo 物件,包含序列化 Dictionary<TKey,TValue> 所需的資訊。

context
StreamingContext

StreamingContext 結構,包含與 Dictionary<TKey,TValue> 相關聯之已序列化資料流的來源和目的地。

屬性

備註

還原串行化期間會呼叫這個建構函式,以重新建構透過數據流傳輸的物件。 如需詳細資訊,請參閱 < XML 和 SOAP 序列化

另請參閱

適用於