Dictionary<TKey,TValue> Oluşturucular

Tanım

Dictionary<TKey,TValue> sınıfının yeni bir örneğini başlatır.

Aşırı Yüklemeler

Name Description
Dictionary<TKey,TValue>()

Sınıfın Dictionary<TKey,TValue> boş, varsayılan başlangıç kapasitesine sahip yeni bir örneğini başlatır ve anahtar türü için varsayılan eşitlik karşılaştırıcısını kullanır.

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

Belirtilen Dictionary<TKey,TValue> öğeden kopyalanan öğeleri içeren sınıfın IDictionary<TKey,TValue> yeni bir örneğini başlatır ve anahtar türü için varsayılan eşitlik karşılaştırıcısını kullanır.

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

Belirtilen Dictionary<TKey,TValue>öğesinden kopyalanan öğeleri içeren sınıfın yeni bir örneğini IEnumerable<T> başlatır.

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

Sınıfın Dictionary<TKey,TValue> boş, varsayılan başlangıç kapasitesine sahip yeni bir örneğini başlatır ve belirtilen IEqualityComparer<T>öğesini kullanır.

Dictionary<TKey,TValue>(Int32)

Sınıfın Dictionary<TKey,TValue> boş, belirtilen ilk kapasiteye sahip yeni bir örneğini başlatır ve anahtar türü için varsayılan eşitlik karşılaştırıcısını kullanır.

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

belirtilen öğesinden kopyalanan öğeleri içeren ve belirtilen Dictionary<TKey,TValue>IDictionary<TKey,TValue>öğesini kullanan sınıfın yeni bir örneğini IEqualityComparer<T> başlatır.

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

belirtilen öğesinden kopyalanan öğeleri içeren ve belirtilen Dictionary<TKey,TValue>IEnumerable<T>öğesini kullanan sınıfın yeni bir örneğini IEqualityComparer<T> başlatır.

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

Boş, belirtilen ilk kapasiteye Dictionary<TKey,TValue> sahip olan ve belirtilen IEqualityComparer<T>öğesini kullanan sınıfın yeni bir örneğini başlatır.

Dictionary<TKey,TValue>(SerializationInfo, StreamingContext)
Geçersiz.

Serileştirilmiş verilerle Dictionary<TKey,TValue> sınıfının yeni bir örneğini başlatır.

Dictionary<TKey,TValue>()

Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs

Sınıfın Dictionary<TKey,TValue> boş, varsayılan başlangıç kapasitesine sahip yeni bir örneğini başlatır ve anahtar türü için varsayılan eşitlik karşılaştırıcısını kullanır.

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

Örnekler

Aşağıdaki kod örneği, dize anahtarlarıyla boş bir Dictionary<TKey,TValue> dize oluşturur ve yöntemini kullanarak Add bazı öğeler ekler. Örnek, yinelenen anahtar eklemeye çalışırken yönteminin bir Add oluşturduğunu ArgumentException gösterir.

Bu kod örneği, sınıfı için Dictionary<TKey,TValue> sağlanan daha büyük bir örneğin parçasıdır.

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

Açıklamalar

içindeki Dictionary<TKey,TValue> her anahtar, varsayılan eşitlik karşılaştırıcısına göre benzersiz olmalıdır.

Dictionary<TKey,TValue> anahtarların eşit olup olmadığını belirlemek için bir eşitlik uygulaması gerektirir. Bu oluşturucu varsayılan genel eşitlik karşılaştırıcısını kullanır. EqualityComparer<T>.Default Tür TKey genel arabirimi uygularsa System.IEquatable<T> , varsayılan eşitlik karşılaştırıcısı bu uygulamayı kullanır. Alternatif olarak, parametre kabul eden bir oluşturucu kullanarak genel arabirimin IEqualityComparer<T> bir comparer uygulamasını belirtebilirsiniz.

Note

Koleksiyonun boyutunu tahmin edebilirseniz, ilk kapasiteyi belirten bir oluşturucu kullanmak, öğesine öğe eklerken bir dizi yeniden boyutlandırma işlemi gerçekleştirme gereksinimini Dictionary<TKey,TValue>ortadan kaldırır.

Bu oluşturucu bir O(1) işlemidir.

Ayrıca bkz.

Şunlara uygulanır

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

Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs

Belirtilen Dictionary<TKey,TValue> öğeden kopyalanan öğeleri içeren sınıfın IDictionary<TKey,TValue> yeni bir örneğini başlatır ve anahtar türü için varsayılan eşitlik karşılaştırıcısını kullanır.

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

Parametreler

dictionary
IDictionary<TKey,TValue>

IDictionary<TKey,TValue> Öğeleri yeni Dictionary<TKey,TValue>öğesine kopyalanan .

Özel durumlar

dictionary, null'e eşittir.

dictionary bir veya daha fazla yinelenen anahtar içerir.

Örnekler

Aşağıdaki kod örneği, başka bir Dictionary<TKey,TValue>(IEqualityComparer<TKey>) sözlükten sıralanmış içerikle bir başlatmak için oluşturucunun Dictionary<TKey,TValue> nasıl kullanılacağını gösterir. Kod örneği bir SortedDictionary<TKey,TValue> oluşturur ve verileri rastgele sırada doldurur, ardından öğesini oluşturucuya SortedDictionary<TKey,TValue> geçirir Dictionary<TKey,TValue>(IEqualityComparer<TKey>) ve sıralanmış bir Dictionary<TKey,TValue> oluşturur. Bu, belirli bir noktada statik hale gelen sıralanmış bir sözlük oluşturmanız gerekiyorsa kullanışlıdır; 'den SortedDictionary<TKey,TValue> bir'e Dictionary<TKey,TValue> veri kopyalamak, alma hızını artırır.

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

// Create a new sorted dictionary of strings, with string
// keys.
let openWith = 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.
let copy = Dictionary<string, string> openWith

// List the contents of the copy.
printfn ""

for kvp in copy do
    printfn $"Key = {kvp.Key}, Value = {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

Açıklamalar

içindeki Dictionary<TKey,TValue> her anahtar varsayılan eşitlik karşılaştırıcısına göre benzersiz olmalıdır; benzer şekilde, kaynaktaki dictionary her anahtar da varsayılan eşitlik karşılaştırıcısına göre benzersiz olmalıdır.

Yeninin Dictionary<TKey,TValue> ilk kapasitesi içindeki tüm öğeleri dictionaryiçerecek kadar büyük.

Dictionary<TKey,TValue> anahtarların eşit olup olmadığını belirlemek için bir eşitlik uygulaması gerektirir. Bu oluşturucu varsayılan genel eşitlik karşılaştırıcısını kullanır. EqualityComparer<T>.Default Tür TKey genel arabirimi uygularsa System.IEquatable<T> , varsayılan eşitlik karşılaştırıcısı bu uygulamayı kullanır. Alternatif olarak, parametre kabul eden bir oluşturucu kullanarak genel arabirimin IEqualityComparer<T> bir comparer uygulamasını belirtebilirsiniz.

Bu oluşturucu bir O(n) işlemidir; burada n, içindeki dictionaryöğelerin sayısıdır.

Ayrıca bkz.

Şunlara uygulanır

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

Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs

Belirtilen Dictionary<TKey,TValue>öğesinden kopyalanan öğeleri içeren sınıfın yeni bir örneğini IEnumerable<T> başlatır.

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

Parametreler

collection
IEnumerable<KeyValuePair<TKey,TValue>>

IEnumerable<T> Öğeleri yeni Dictionary<TKey,TValue>öğesine kopyalanan .

Özel durumlar

collection, null'e eşittir.

collection bir veya daha fazla yinelenen anahtar içerir.

Şunlara uygulanır

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

Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs

Sınıfın Dictionary<TKey,TValue> boş, varsayılan başlangıç kapasitesine sahip yeni bir örneğini başlatır ve belirtilen IEqualityComparer<T>öğesini kullanır.

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

Parametreler

comparer
IEqualityComparer<TKey>

IEqualityComparer<T> Anahtarları karşılaştırırken kullanılacak veya null anahtarın türü için varsayılan EqualityComparer<T> değerin kullanılacağı uygulama.

Örnekler

Aşağıdaki kod örneği, geçerli kültür için büyük/küçük harfe duyarsız eşitlik karşılaştırıcısı içeren bir Dictionary<TKey,TValue> oluşturur. Örnek, bazıları küçük harfli, bazıları büyük harf anahtarlı dört öğe ekler. Örnek daha sonra yalnızca büyük/küçük harfe göre mevcut anahtardan farklı bir anahtara sahip bir öğe eklemeyi dener, sonuçta elde edilen özel durumu yakalar ve bir hata iletisi görüntüler. Son olarak, örnek sözlükteki öğeleri görüntüler.

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

// Create a new Dictionary of strings, with string keys
// and a case-insensitive comparer for the current culture.
let openWith = 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")
with :? ArgumentException ->
    printfn "\nBMP is already in the dictionary."

// List the contents of the sorted dictionary.
printfn ""

for kvp in openWith do
    printfn $"Key = {kvp.Key}, Value = {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

Açıklamalar

Büyük/küçük harfe duyarlı olmayan dize anahtarlarıyla sözlükler oluşturmak için sınıfı tarafından StringComparer sağlanan büyük/küçük harfe duyarlı olmayan dize karşılaştırıcılarıyla bu oluşturucuyu kullanın.

içindeki Dictionary<TKey,TValue> her anahtar, belirtilen karşılaştırıcıya göre benzersiz olmalıdır.

Dictionary<TKey,TValue> anahtarların eşit olup olmadığını belirlemek için bir eşitlik uygulaması gerektirir. ise comparernull, bu oluşturucu varsayılan genel eşitlik karşılaştırıcısını EqualityComparer<T>.Defaultkullanır. Tür TKey genel arabirimi uygularsa System.IEquatable<T> , varsayılan eşitlik karşılaştırıcısı bu uygulamayı kullanır.

Note

Koleksiyonun boyutunu tahmin edebilirseniz, ilk kapasiteyi belirten bir oluşturucu kullanmak, öğesine öğe eklerken bir dizi yeniden boyutlandırma işlemi gerçekleştirme gereksinimini Dictionary<TKey,TValue>ortadan kaldırır.

Bu oluşturucu bir O(1) işlemidir.

Caution

Kullanıcı girişinden geliyorsa capacity , kapasite parametresi olmayan bir oluşturucu kullanmayı ve öğeler eklendikçe koleksiyonun yeniden boyutlandırılmalarını sağlamayı tercih edin. Kullanıcı tarafından belirtilen bir değer kullanmanız gerekiyorsa, bunu makul bir sınıra (örneğin, Math.Clamp(untrustedValue, 0, 20)) sıkıştırın veya öğe sayısının belirtilen değerle eşleşip eşleşmediğini doğrulayın.

Ayrıca bkz.

Şunlara uygulanır

Dictionary<TKey,TValue>(Int32)

Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs

Sınıfın Dictionary<TKey,TValue> boş, belirtilen ilk kapasiteye sahip yeni bir örneğini başlatır ve anahtar türü için varsayılan eşitlik karşılaştırıcısını kullanır.

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)

Parametreler

capacity
Int32

öğesinin içerebileceği ilk öğe Dictionary<TKey,TValue> sayısı.

Özel durumlar

capacity 0'dan küçüktür.

Örnekler

Aşağıdaki kod örneği, ilk kapasitesi 4 olan bir sözlük oluşturur ve bunu 4 girişle doldurur.

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

// Create a new dictionary of strings, with string keys and
// an initial capacity of 4.
let openWith = 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.
printfn ""

for kvp in openWith do
    printfn $"Key = {kvp.Key}, Value = {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

Açıklamalar

içindeki Dictionary<TKey,TValue> her anahtar, varsayılan eşitlik karşılaştırıcısına göre benzersiz olmalıdır.

'nin Dictionary<TKey,TValue> kapasitesi, yeniden boyutlandırma gerekmeden önce öğesine Dictionary<TKey,TValue> eklenebilecek öğe sayısıdır. öğesine öğeler eklendikçe Dictionary<TKey,TValue>, iç dizi yeniden konumlandırılarak kapasite gerektiği gibi otomatik olarak artırılır.

Koleksiyonun boyutu tahmin edilebiliyorsa, ilk kapasitenin belirtilmesi, öğesine öğe eklerken bir dizi yeniden boyutlandırma işlemi gerçekleştirme gereksinimini Dictionary<TKey,TValue>ortadan kaldırır.

Dictionary<TKey,TValue> anahtarların eşit olup olmadığını belirlemek için bir eşitlik uygulaması gerektirir. Bu oluşturucu varsayılan genel eşitlik karşılaştırıcısını kullanır. EqualityComparer<T>.Default Tür TKey genel arabirimi uygularsa System.IEquatable<T> , varsayılan eşitlik karşılaştırıcısı bu uygulamayı kullanır. Alternatif olarak, parametre kabul eden bir oluşturucu kullanarak genel arabirimin IEqualityComparer<T> bir comparer uygulamasını belirtebilirsiniz.

Bu oluşturucu bir O(1) işlemidir.

Caution

Kullanıcı girişinden geliyorsa capacity , kapasite parametresi olmayan bir oluşturucu kullanmayı ve öğeler eklendikçe koleksiyonun yeniden boyutlandırılmalarını sağlamayı tercih edin. Kullanıcı tarafından belirtilen bir değer kullanmanız gerekiyorsa, bunu makul bir sınıra (örneğin, Math.Clamp(untrustedValue, 0, 20)) sıkıştırın veya öğe sayısının belirtilen değerle eşleşip eşleşmediğini doğrulayın.

Ayrıca bkz.

Şunlara uygulanır

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

Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs

belirtilen öğesinden kopyalanan öğeleri içeren ve belirtilen Dictionary<TKey,TValue>IDictionary<TKey,TValue>öğesini kullanan sınıfın yeni bir örneğini IEqualityComparer<T> başlatır.

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

Parametreler

dictionary
IDictionary<TKey,TValue>

IDictionary<TKey,TValue> Öğeleri yeni Dictionary<TKey,TValue>öğesine kopyalanan .

comparer
IEqualityComparer<TKey>

IEqualityComparer<T> Anahtarları karşılaştırırken kullanılacak veya null anahtarın türü için varsayılan EqualityComparer<T> değerin kullanılacağı uygulama.

Özel durumlar

dictionary, null'e eşittir.

dictionary bir veya daha fazla yinelenen anahtar içerir.

Örnekler

Aşağıdaki kod örneği, başka bir Dictionary<TKey,TValue>(IDictionary<TKey,TValue>, IEqualityComparer<TKey>) sözlükten Dictionary<TKey,TValue> büyük/küçük harfe duyarlı olmayan sıralanmış içerik başlatmak için oluşturucunun nasıl kullanılacağını gösterir. Kod örneği, büyük/küçük harfe duyarlı olmayan bir karşılaştırıcı ile bir SortedDictionary<TKey,TValue> oluşturur ve bunu rastgele sırada verilerle doldurur, ardından öğesini oluşturucuya SortedDictionary<TKey,TValue> iletir Dictionary<TKey,TValue>(IDictionary<TKey,TValue>, IEqualityComparer<TKey>) ve büyük/küçük harfe duyarsız eşitlik karşılaştırıcısı ile birlikte sıralanmış bir Dictionary<TKey,TValue> değer oluşturur. Bu, belirli bir noktada statik hale gelen sıralanmış bir sözlük oluşturmanız gerekiyorsa kullanışlıdır; 'den SortedDictionary<TKey,TValue> bir'e Dictionary<TKey,TValue> veri kopyalamak, alma hızını artırır.

Note

Büyük/küçük harfe duyarlı olmayan bir karşılaştırıcı ile yeni bir sözlük oluşturduğunuzda ve bunu büyük/küçük harfe duyarlı karşılaştırıcı kullanan bir sözlükteki girdilerle doldurduğunuz zaman, bu örnekte olduğu gibi, giriş sözlüğünde yalnızca büyük/küçük harfe göre farklılık gösteren anahtarlar varsa bir özel durum oluşur.

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

// Create a new sorted dictionary of strings, with string
// keys and a case-insensitive comparer.
let openWith =
    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.
let copy =
    Dictionary<string, string>(openWith, StringComparer.CurrentCultureIgnoreCase)

// List the contents of the copy.
printfn ""

for kvp in copy do
    printfn $"Key = {kvp.Key}, Value = {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

Açıklamalar

Büyük/küçük harfe duyarlı olmayan dize anahtarlarıyla sözlükler oluşturmak için sınıfı tarafından StringComparer sağlanan büyük/küçük harfe duyarlı olmayan dize karşılaştırıcılarıyla bu oluşturucuyu kullanın.

içindeki Dictionary<TKey,TValue> her anahtar, belirtilen karşılaştırıcıya göre benzersiz olmalıdır; benzer şekilde, kaynaktaki dictionary her anahtar da belirtilen karşılaştırıcıya göre benzersiz olmalıdır.

Note

Örneğin, sınıfı tarafından comparer sağlanan büyük/küçük harfe duyarlı olmayan dize karşılaştırıcılarından biriyse ve StringComparer büyük/küçük harfe duyarlı olmayan bir karşılaştırıcı anahtarı kullanmıyorsadictionary, yinelenen anahtarlar oluşabilir.

Yeninin Dictionary<TKey,TValue> ilk kapasitesi içindeki tüm öğeleri dictionaryiçerecek kadar büyük.

Dictionary<TKey,TValue> anahtarların eşit olup olmadığını belirlemek için bir eşitlik uygulaması gerektirir. ise comparernull, bu oluşturucu varsayılan genel eşitlik karşılaştırıcısını EqualityComparer<T>.Defaultkullanır. Tür TKey genel arabirimi uygularsa System.IEquatable<T> , varsayılan eşitlik karşılaştırıcısı bu uygulamayı kullanır.

Bu oluşturucu, içindeki nöğelerin sayısı olan n bir O(dictionary) işlemidir.

Ayrıca bkz.

Şunlara uygulanır

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

Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs

belirtilen öğesinden kopyalanan öğeleri içeren ve belirtilen Dictionary<TKey,TValue>IEnumerable<T>öğesini kullanan sınıfın yeni bir örneğini IEqualityComparer<T> başlatır.

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

Parametreler

collection
IEnumerable<KeyValuePair<TKey,TValue>>

IEnumerable<T> Öğeleri yeni Dictionary<TKey,TValue>öğesine kopyalanan .

comparer
IEqualityComparer<TKey>

IEqualityComparer<T> Anahtarları karşılaştırırken kullanılacak veya null anahtarın türü için varsayılan EqualityComparer<T> değerin kullanılacağı uygulama.

Özel durumlar

collection, null'e eşittir.

collection bir veya daha fazla yinelenen anahtar içerir.

Şunlara uygulanır

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

Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs

Boş, belirtilen ilk kapasiteye Dictionary<TKey,TValue> sahip olan ve belirtilen IEqualityComparer<T>öğesini kullanan sınıfın yeni bir örneğini başlatır.

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

Parametreler

capacity
Int32

öğesinin içerebileceği ilk öğe Dictionary<TKey,TValue> sayısı.

comparer
IEqualityComparer<TKey>

IEqualityComparer<T> Anahtarları karşılaştırırken kullanılacak veya null anahtarın türü için varsayılan EqualityComparer<T> değerin kullanılacağı uygulama.

Özel durumlar

capacity 0'dan küçüktür.

Örnekler

Aşağıdaki kod örneği, ilk kapasitesi 5 olan bir ve geçerli kültür için büyük/küçük harfe duyarsız eşitlik karşılaştırıcısı oluşturur Dictionary<TKey,TValue> . Örnek, bazıları küçük harfli, bazıları büyük harf anahtarlı dört öğe ekler. Örnek daha sonra yalnızca büyük/küçük harfe göre mevcut anahtardan farklı bir anahtara sahip bir öğe eklemeyi dener, sonuçta elde edilen özel durumu yakalar ve bir hata iletisi görüntüler. Son olarak, örnek sözlükteki öğeleri görüntüler.

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

// Create a new dictionary of strings, with string keys, an
// initial capacity of 5, and a case-insensitive equality
// comparer.
let openWith =
    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")
with :? ArgumentException ->
    printfn "\nBMP is already in the dictionary."

// List the contents of the dictionary.
printfn ""

for kvp in openWith do
    printfn $"Key = {kvp.Key}, Value = {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

Açıklamalar

Büyük/küçük harfe duyarlı olmayan dize anahtarlarıyla sözlükler oluşturmak için sınıfı tarafından StringComparer sağlanan büyük/küçük harfe duyarlı olmayan dize karşılaştırıcılarıyla bu oluşturucuyu kullanın.

içindeki Dictionary<TKey,TValue> her anahtar, belirtilen karşılaştırıcıya göre benzersiz olmalıdır.

'nin Dictionary<TKey,TValue> kapasitesi, yeniden boyutlandırma gerekmeden önce öğesine Dictionary<TKey,TValue> eklenebilecek öğe sayısıdır. öğesine öğeler eklendikçe Dictionary<TKey,TValue>, iç dizi yeniden konumlandırılarak kapasite gerektiği gibi otomatik olarak artırılır.

Koleksiyonun boyutu tahmin edilebiliyorsa, ilk kapasitenin belirtilmesi, öğesine öğe eklerken bir dizi yeniden boyutlandırma işlemi gerçekleştirme gereksinimini Dictionary<TKey,TValue>ortadan kaldırır.

Dictionary<TKey,TValue> anahtarların eşit olup olmadığını belirlemek için bir eşitlik uygulaması gerektirir. ise comparernull, bu oluşturucu varsayılan genel eşitlik karşılaştırıcısını EqualityComparer<T>.Defaultkullanır. Tür TKey genel arabirimi uygularsa System.IEquatable<T> , varsayılan eşitlik karşılaştırıcısı bu uygulamayı kullanır.

Bu oluşturucu bir O(1) işlemidir.

Ayrıca bkz.

Şunlara uygulanır

Dictionary<TKey,TValue>(SerializationInfo, StreamingContext)

Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs
Kaynak:
Dictionary.cs

Dikkat

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

Serileştirilmiş verilerle Dictionary<TKey,TValue> sınıfının yeni bir örneğini başlatır.

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);
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}")>]
new System.Collections.Generic.Dictionary<'Key, 'Value> : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Collections.Generic.Dictionary<'Key, 'Value>
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)

Parametreler

info
SerializationInfo

serileştirmek SerializationInfoDictionary<TKey,TValue>için gereken bilgileri içeren bir nesne.

context
StreamingContext

StreamingContext ile ilişkilendirilmiş serileştirilmiş akışın kaynağını ve hedefini Dictionary<TKey,TValue>içeren bir yapı.

Öznitelikler

Açıklamalar

Bu oluşturucu, bir akış üzerinden iletilen bir nesneyi yeniden oluşturmak için seri durumdan çıkarma sırasında çağrılır. Daha fazla bilgi için bkz. XML ve SOAP Serileştirme.

Ayrıca bkz.

Şunlara uygulanır