Dictionary<TKey,TValue> Konstruktor

Definisi

Menginisialisasi instans baru kelas Dictionary<TKey,TValue>.

Overload

Dictionary<TKey,TValue>()

Menginisialisasi instans Dictionary<TKey,TValue> baru kelas yang kosong, memiliki kapasitas awal default, dan menggunakan perbandingan kesetaraan default untuk jenis kunci.

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

Menginisialisasi instans Dictionary<TKey,TValue> baru kelas yang berisi elemen yang disalin dari yang ditentukan dan menggunakan perbandingan IDictionary<TKey,TValue> kesetaraan default untuk jenis kunci.

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

Menginisialisasi instans Dictionary<TKey,TValue> baru kelas yang berisi elemen yang disalin dari yang ditentukan IEnumerable<T>.

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

Menginisialisasi instans Dictionary<TKey,TValue> baru kelas yang kosong, memiliki kapasitas awal default, dan menggunakan yang ditentukan IEqualityComparer<T>.

Dictionary<TKey,TValue>(Int32)

Menginisialisasi instans Dictionary<TKey,TValue> baru kelas yang kosong, memiliki kapasitas awal yang ditentukan, dan menggunakan perbandingan kesetaraan default untuk jenis kunci.

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

Menginisialisasi instans Dictionary<TKey,TValue> baru kelas yang berisi elemen yang disalin dari yang ditentukan IDictionary<TKey,TValue> dan menggunakan IEqualityComparer<T>.

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

Menginisialisasi instans Dictionary<TKey,TValue> baru kelas yang berisi elemen yang disalin dari yang ditentukan IEnumerable<T> dan menggunakan IEqualityComparer<T>.

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

Menginisialisasi instans Dictionary<TKey,TValue> baru kelas yang kosong, memiliki kapasitas awal yang ditentukan, dan menggunakan yang ditentukan IEqualityComparer<T>.

Dictionary<TKey,TValue>(SerializationInfo, StreamingContext)
Kedaluwarsa.

Menginisialisasi instans Dictionary<TKey,TValue> baru kelas dengan data berseri.

Dictionary<TKey,TValue>()

Sumber:
Dictionary.cs
Sumber:
Dictionary.cs
Sumber:
Dictionary.cs

Menginisialisasi instans Dictionary<TKey,TValue> baru kelas yang kosong, memiliki kapasitas awal default, dan menggunakan perbandingan kesetaraan default untuk jenis kunci.

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

Contoh

Contoh kode berikut membuat string kosong Dictionary<TKey,TValue> dengan kunci string dan menggunakan Add metode untuk menambahkan beberapa elemen. Contoh menunjukkan bahwa Add metode melemparkan ArgumentException saat mencoba menambahkan kunci duplikat.

Contoh kode ini adalah bagian dari contoh yang lebih besar yang disediakan untuk Dictionary<TKey,TValue> kelas .

// 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

Keterangan

Setiap kunci dalam Dictionary<TKey,TValue> harus unik sesuai dengan perbandingan kesetaraan default.

Dictionary<TKey,TValue> memerlukan implementasi kesetaraan untuk menentukan apakah kunci sama. Konstruktor ini menggunakan perbandingan kesetaraan generik default, EqualityComparer<T>.Default. Jika jenis TKey mengimplementasikan System.IEquatable<T> antarmuka generik, perbandingan kesetaraan default menggunakan implementasi tersebut. Atau, Anda dapat menentukan implementasi IEqualityComparer<T> antarmuka generik dengan menggunakan konstruktor yang menerima comparer parameter.

Catatan

Jika Anda dapat memperkirakan ukuran koleksi, menggunakan konstruktor yang menentukan kapasitas awal menghilangkan kebutuhan untuk melakukan sejumlah operasi pengubahan ukuran saat menambahkan elemen ke Dictionary<TKey,TValue>.

Konstruktor ini adalah operasi O(1).

Lihat juga

Berlaku untuk

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

Sumber:
Dictionary.cs
Sumber:
Dictionary.cs
Sumber:
Dictionary.cs

Menginisialisasi instans Dictionary<TKey,TValue> baru kelas yang berisi elemen yang disalin dari yang ditentukan dan menggunakan perbandingan IDictionary<TKey,TValue> kesetaraan default untuk jenis kunci.

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

Parameter

dictionary
IDictionary<TKey,TValue>

Elemen IDictionary<TKey,TValue> yang disalin ke yang baru Dictionary<TKey,TValue>.

Pengecualian

dictionaryadalah null.

dictionary berisi satu atau beberapa kunci duplikat.

Contoh

Contoh kode berikut menunjukkan cara menggunakan Dictionary<TKey,TValue>(IEqualityComparer<TKey>) konstruktor untuk menginisialisasi dengan konten yang Dictionary<TKey,TValue> diurutkan dari kamus lain. Contoh kode membuat SortedDictionary<TKey,TValue> dan mengisinya dengan data dalam urutan acak, lalu meneruskan SortedDictionary<TKey,TValue> ke Dictionary<TKey,TValue>(IEqualityComparer<TKey>) konstruktor, membuat Dictionary<TKey,TValue> yang diurutkan. Ini berguna jika Anda perlu membangun kamus yang diurutkan yang pada titik tertentu menjadi statis; menyalin data dari SortedDictionary<TKey,TValue> ke meningkatkan Dictionary<TKey,TValue> kecepatan pengambilan.

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

Keterangan

Setiap kunci dalam Dictionary<TKey,TValue> harus unik sesuai dengan perbandingan kesetaraan default; demikian juga, setiap kunci dalam sumber dictionary juga harus unik sesuai dengan perbandingan kesetaraan default.

Kapasitas awal yang baru Dictionary<TKey,TValue> cukup besar untuk berisi semua elemen di dictionary.

Dictionary<TKey,TValue> memerlukan implementasi kesetaraan untuk menentukan apakah kunci sama. Konstruktor ini menggunakan perbandingan kesetaraan generik default, EqualityComparer<T>.Default. Jika jenis TKey mengimplementasikan System.IEquatable<T> antarmuka generik, perbandingan kesetaraan default menggunakan implementasi tersebut. Atau, Anda dapat menentukan implementasi IEqualityComparer<T> antarmuka generik dengan menggunakan konstruktor yang menerima comparer parameter.

Konstruktor ini adalah operasi O(n), di mana n adalah jumlah elemen dalam dictionary.

Lihat juga

Berlaku untuk

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

Sumber:
Dictionary.cs
Sumber:
Dictionary.cs
Sumber:
Dictionary.cs

Menginisialisasi instans Dictionary<TKey,TValue> baru kelas yang berisi elemen yang disalin dari yang ditentukan 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)))

Parameter

collection
IEnumerable<KeyValuePair<TKey,TValue>>

Elemen IEnumerable<T> yang disalin ke yang baru Dictionary<TKey,TValue>.

Pengecualian

collectionadalah null.

collection berisi satu atau beberapa kunci duplikat.

Berlaku untuk

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

Sumber:
Dictionary.cs
Sumber:
Dictionary.cs
Sumber:
Dictionary.cs

Menginisialisasi instans Dictionary<TKey,TValue> baru kelas yang kosong, memiliki kapasitas awal default, dan menggunakan yang ditentukan 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))

Parameter

comparer
IEqualityComparer<TKey>

Implementasi IEqualityComparer<T> yang digunakan saat membandingkan kunci, atau null menggunakan default EqualityComparer<T> untuk jenis kunci.

Contoh

Contoh kode berikut membuat Dictionary<TKey,TValue> dengan perbandingan kesetaraan yang tidak peka huruf besar/kecil untuk budaya saat ini. Contohnya menambahkan empat elemen, beberapa dengan kunci huruf kecil dan beberapa dengan kunci huruf besar. Contoh kemudian mencoba menambahkan elemen dengan kunci yang berbeda dari kunci yang ada hanya berdasarkan kasus, menangkap pengecualian yang dihasilkan, dan menampilkan pesan kesalahan. Terakhir, contoh menampilkan elemen dalam kamus.

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

Keterangan

Gunakan konstruktor ini dengan pembanding string yang tidak peka huruf besar/kecil yang disediakan oleh StringComparer kelas untuk membuat kamus dengan kunci string yang tidak peka huruf besar/kecil.

Setiap kunci dalam Dictionary<TKey,TValue> harus unik sesuai dengan perbandingan yang ditentukan.

Dictionary<TKey,TValue> memerlukan implementasi kesetaraan untuk menentukan apakah kunci sama. Jika comparer adalah null, konstruktor ini menggunakan perbandingan kesetaraan generik default, EqualityComparer<T>.Default. Jika jenis TKey mengimplementasikan System.IEquatable<T> antarmuka generik, perbandingan kesetaraan default menggunakan implementasi tersebut.

Catatan

Jika Anda dapat memperkirakan ukuran koleksi, menggunakan konstruktor yang menentukan kapasitas awal menghilangkan kebutuhan untuk melakukan sejumlah operasi pengubahan ukuran saat menambahkan elemen ke Dictionary<TKey,TValue>.

Konstruktor ini adalah operasi O(1).

Lihat juga

Berlaku untuk

Dictionary<TKey,TValue>(Int32)

Sumber:
Dictionary.cs
Sumber:
Dictionary.cs
Sumber:
Dictionary.cs

Menginisialisasi instans Dictionary<TKey,TValue> baru kelas yang kosong, memiliki kapasitas awal yang ditentukan, dan menggunakan perbandingan kesetaraan default untuk jenis kunci.

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)

Parameter

capacity
Int32

Jumlah awal elemen yang dapat dikandung Dictionary<TKey,TValue> .

Pengecualian

capacity kurang dari 0.

Contoh

Contoh kode berikut membuat kamus dengan kapasitas awal 4 dan mengisinya dengan 4 entri.

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

Keterangan

Setiap kunci dalam Dictionary<TKey,TValue> harus unik sesuai dengan perbandingan kesetaraan default.

Kapasitas adalah Dictionary<TKey,TValue> jumlah elemen yang dapat ditambahkan ke Dictionary<TKey,TValue> sebelum mengubah ukuran diperlukan. Saat elemen ditambahkan ke Dictionary<TKey,TValue>, kapasitas secara otomatis ditingkatkan sesuai kebutuhan dengan merealokasi array internal.

Jika ukuran koleksi dapat diperkirakan, menentukan kapasitas awal menghilangkan kebutuhan untuk melakukan sejumlah operasi pengubahan ukuran sambil menambahkan elemen ke Dictionary<TKey,TValue>.

Dictionary<TKey,TValue> memerlukan implementasi kesetaraan untuk menentukan apakah kunci sama. Konstruktor ini menggunakan perbandingan kesetaraan generik default, EqualityComparer<T>.Default. Jika jenis TKey mengimplementasikan System.IEquatable<T> antarmuka generik, perbandingan kesetaraan default menggunakan implementasi tersebut. Atau, Anda dapat menentukan implementasi IEqualityComparer<T> antarmuka generik dengan menggunakan konstruktor yang menerima comparer parameter.

Konstruktor ini adalah operasi O(1).

Lihat juga

Berlaku untuk

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

Sumber:
Dictionary.cs
Sumber:
Dictionary.cs
Sumber:
Dictionary.cs

Menginisialisasi instans Dictionary<TKey,TValue> baru kelas yang berisi elemen yang disalin dari yang ditentukan IDictionary<TKey,TValue> dan menggunakan 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))

Parameter

dictionary
IDictionary<TKey,TValue>

Elemen IDictionary<TKey,TValue> yang disalin ke yang baru Dictionary<TKey,TValue>.

comparer
IEqualityComparer<TKey>

Implementasi IEqualityComparer<T> yang digunakan saat membandingkan kunci, atau null menggunakan default EqualityComparer<T> untuk jenis kunci.

Pengecualian

dictionaryadalah null.

dictionary berisi satu atau beberapa kunci duplikat.

Contoh

Contoh kode berikut menunjukkan cara menggunakan Dictionary<TKey,TValue>(IDictionary<TKey,TValue>, IEqualityComparer<TKey>) konstruktor untuk menginisialisasi Dictionary<TKey,TValue> dengan konten yang tidak peka huruf besar/kecil dari kamus lain. Contoh kode membuat SortedDictionary<TKey,TValue> dengan perbandingan yang tidak peka huruf besar/kecil dan mengisinya dengan data dalam urutan acak, lalu meneruskan SortedDictionary<TKey,TValue> ke konstruktor, bersama dengan perbandingan kesetaraan Dictionary<TKey,TValue>(IDictionary<TKey,TValue>, IEqualityComparer<TKey>) yang tidak peka huruf besar/kecil, membuat Dictionary<TKey,TValue> yang diurutkan. Ini berguna jika Anda perlu membangun kamus yang diurutkan yang pada titik tertentu menjadi statis; menyalin data dari SortedDictionary<TKey,TValue> ke meningkatkan Dictionary<TKey,TValue> kecepatan pengambilan.

Catatan

Saat Anda membuat kamus baru dengan pembanding yang tidak peka huruf besar/kecil dan mengisinya dengan entri dari kamus yang menggunakan perbandingan peka huruf besar/kecil, seperti dalam contoh ini, pengecualian terjadi jika kamus input memiliki kunci yang hanya berbeda menurut kasus.

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

Keterangan

Gunakan konstruktor ini dengan pembanding string yang tidak peka huruf besar/kecil yang disediakan oleh StringComparer kelas untuk membuat kamus dengan kunci string yang tidak peka huruf besar/kecil.

Setiap kunci dalam Dictionary<TKey,TValue> harus unik sesuai dengan perbandingan yang ditentukan; demikian juga, setiap kunci dalam sumber dictionary juga harus unik sesuai dengan perbandingan yang ditentukan.

Catatan

Misalnya, kunci duplikat dapat terjadi jika comparer merupakan salah satu pembanding string yang tidak peka huruf besar/kecil yang disediakan oleh StringComparer kelas dan dictionary tidak menggunakan kunci pembanding yang tidak peka huruf besar/kecil.

Kapasitas awal yang baru Dictionary<TKey,TValue> cukup besar untuk berisi semua elemen di dictionary.

Dictionary<TKey,TValue> memerlukan implementasi kesetaraan untuk menentukan apakah kunci sama. Jika comparer adalah null, konstruktor ini menggunakan perbandingan kesetaraan generik default, EqualityComparer<T>.Default. Jika jenis TKey mengimplementasikan System.IEquatable<T> antarmuka generik, perbandingan kesetaraan default menggunakan implementasi tersebut.

Konstruktor ini adalah operasi O(n), di mana n adalah jumlah elemen dalam dictionary.

Lihat juga

Berlaku untuk

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

Sumber:
Dictionary.cs
Sumber:
Dictionary.cs
Sumber:
Dictionary.cs

Menginisialisasi instans Dictionary<TKey,TValue> baru kelas yang berisi elemen yang disalin dari yang ditentukan IEnumerable<T> dan menggunakan yang ditentukan 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))

Parameter

collection
IEnumerable<KeyValuePair<TKey,TValue>>

Elemen IEnumerable<T> yang disalin ke yang baru Dictionary<TKey,TValue>.

comparer
IEqualityComparer<TKey>

Implementasi IEqualityComparer<T> yang digunakan saat membandingkan kunci, atau null menggunakan default EqualityComparer<T> untuk jenis kunci.

Pengecualian

collectionadalah null.

collection berisi satu atau beberapa kunci duplikat.

Berlaku untuk

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

Sumber:
Dictionary.cs
Sumber:
Dictionary.cs
Sumber:
Dictionary.cs

Menginisialisasi instans baru kelas Dictionary<TKey,TValue> yang kosong, memiliki kapasitas awal yang ditentukan, dan menggunakan yang ditentukan 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))

Parameter

capacity
Int32

Jumlah awal elemen yang dapat dimuat Dictionary<TKey,TValue> .

comparer
IEqualityComparer<TKey>

Implementasi IEqualityComparer<T> yang digunakan saat membandingkan kunci, atau null menggunakan default EqualityComparer<T> untuk jenis kunci.

Pengecualian

capacity kurang dari 0.

Contoh

Contoh kode berikut membuat Dictionary<TKey,TValue> dengan kapasitas awal 5 dan perbandingan kesetaraan yang tidak peka huruf besar/kecil untuk budaya saat ini. Contohnya menambahkan empat elemen, beberapa dengan kunci huruf kecil dan beberapa dengan kunci huruf besar. Contoh kemudian mencoba menambahkan elemen dengan kunci yang berbeda dari kunci yang ada hanya berdasarkan kasus, menangkap pengecualian yang dihasilkan, dan menampilkan pesan kesalahan. Terakhir, contoh menampilkan elemen dalam kamus.

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

Keterangan

Gunakan konstruktor ini dengan pembanding string yang tidak peka huruf besar/kecil yang disediakan oleh StringComparer kelas untuk membuat kamus dengan kunci string yang tidak peka huruf besar/kecil.

Setiap kunci dalam Dictionary<TKey,TValue> harus unik sesuai dengan pembanding yang ditentukan.

Kapasitas adalah Dictionary<TKey,TValue> jumlah elemen yang dapat ditambahkan ke Dictionary<TKey,TValue> sebelum mengubah ukuran diperlukan. Saat elemen ditambahkan ke Dictionary<TKey,TValue>, kapasitas secara otomatis ditingkatkan sesuai kebutuhan dengan merealokasi array internal.

Jika ukuran koleksi dapat diperkirakan, menentukan kapasitas awal menghilangkan kebutuhan untuk melakukan sejumlah operasi pengubahan ukuran sambil menambahkan elemen ke Dictionary<TKey,TValue>.

Dictionary<TKey,TValue> memerlukan implementasi kesetaraan untuk menentukan apakah kunci sama. Jika comparer adalah null, konstruktor ini menggunakan perbandingan kesetaraan generik default, EqualityComparer<T>.Default. Jika jenis TKey mengimplementasikan System.IEquatable<T> antarmuka generik, perbandingan kesetaraan default menggunakan implementasi tersebut.

Konstruktor ini adalah operasi O(1).

Lihat juga

Berlaku untuk

Dictionary<TKey,TValue>(SerializationInfo, StreamingContext)

Sumber:
Dictionary.cs
Sumber:
Dictionary.cs
Sumber:
Dictionary.cs

Perhatian

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

Menginisialisasi instans Dictionary<TKey,TValue> baru kelas dengan data berseri.

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)

Parameter

info
SerializationInfo

Objek SerializationInfo yang berisi informasi yang diperlukan untuk menserialisasikan Dictionary<TKey,TValue>.

context
StreamingContext

StreamingContext Struktur yang berisi sumber dan tujuan aliran berseri yang terkait dengan Dictionary<TKey,TValue>.

Atribut

Keterangan

Konstruktor ini dipanggil selama deserialisasi untuk menyusun ulang objek yang dikirimkan melalui aliran. Untuk informasi selengkapnya, lihat Serialisasi XML dan SOAP.

Lihat juga

Berlaku untuk