Dictionary<TKey,TValue> Konstruktoren
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Initialisiert eine neue Instanz der Dictionary<TKey,TValue>-Klasse.
Überlädt
| Name | Beschreibung |
|---|---|
| Dictionary<TKey,TValue>() |
Initialisiert eine neue Instanz der klasse, die Dictionary<TKey,TValue> leer ist, hat die Standard-Anfangskapazität und verwendet den Standardgleichgleichsvergleich für den Schlüsseltyp. |
| Dictionary<TKey,TValue>(IDictionary<TKey,TValue>) |
Initialisiert eine neue Instanz der Dictionary<TKey,TValue> Klasse, die Elemente enthält, die aus dem angegebenen Element IDictionary<TKey,TValue> kopiert wurden, und verwendet den Standardmäßigen Gleichheitsvergleich für den Schlüsseltyp. |
| Dictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>) |
Initialisiert eine neue Instanz der Dictionary<TKey,TValue> Klasse, die Elemente enthält, die aus dem angegebenen Element kopiert wurden IEnumerable<T>. |
| Dictionary<TKey,TValue>(IEqualityComparer<TKey>) |
Initialisiert eine neue Instanz der klasse, die Dictionary<TKey,TValue> leer ist, hat die Standard-Anfangskapazität und verwendet die angegebene IEqualityComparer<T>. |
| Dictionary<TKey,TValue>(Int32) |
Initialisiert eine neue Instanz der klasse, die Dictionary<TKey,TValue> leer ist, die angegebene Anfangskapazität hat und den Standardgleichgleichsvergleich für den Schlüsseltyp verwendet. |
| Dictionary<TKey,TValue>(IDictionary<TKey,TValue>, IEqualityComparer<TKey>) |
Initialisiert eine neue Instanz der Dictionary<TKey,TValue> Klasse, die Elemente enthält, die aus dem angegebenen Element IDictionary<TKey,TValue> kopiert wurden, und verwendet die angegebene IEqualityComparer<T>. |
| Dictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>, IEqualityComparer<TKey>) |
Initialisiert eine neue Instanz der Dictionary<TKey,TValue> Klasse, die Elemente enthält, die aus dem angegebenen Element IEnumerable<T> kopiert wurden, und verwendet die angegebene IEqualityComparer<T>. |
| Dictionary<TKey,TValue>(Int32, IEqualityComparer<TKey>) |
Initialisiert eine neue Instanz der klasse, die Dictionary<TKey,TValue> leer ist, die angegebene Anfangskapazität hat und verwendet die angegebene IEqualityComparer<T>. |
| Dictionary<TKey,TValue>(SerializationInfo, StreamingContext) |
Veraltet.
Initialisiert eine neue Instanz der Dictionary<TKey,TValue> Klasse mit serialisierten Daten. |
Dictionary<TKey,TValue>()
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
Initialisiert eine neue Instanz der klasse, die Dictionary<TKey,TValue> leer ist, hat die Standard-Anfangskapazität und verwendet den Standardgleichgleichsvergleich für den Schlüsseltyp.
public:
Dictionary();
public Dictionary();
Public Sub New ()
Beispiele
Im folgenden Codebeispiel wird eine leere Dictionary<TKey,TValue> Zeichenfolge mit Zeichenfolgenschlüsseln erstellt und die Add Methode verwendet, um einige Elemente hinzuzufügen. Das Beispiel zeigt, dass die Add Methode beim Versuch, einen doppelten Schlüssel hinzuzufügen, ausgelöst ArgumentException wird.
Dieses Codebeispiel ist Teil eines größeren Beispiels, das für die Dictionary<TKey,TValue> Klasse bereitgestellt wird.
// 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.
let openWith = 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")
with :? ArgumentException ->
printfn "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
Hinweise
Jeder Schlüssel in einem Dictionary<TKey,TValue> muss gemäß dem Standardgleichstellungsvergleich eindeutig sein.
Dictionary<TKey,TValue> erfordert eine Gleichheitsimplementierung, um zu bestimmen, ob Schlüssel gleich sind. Dieser Konstruktor verwendet den standardmäßigen generischen Gleichheitsvergleich. EqualityComparer<T>.Default Wenn der Typ TKey die System.IEquatable<T> generische Schnittstelle implementiert, verwendet der Standardgleichheitsvergleich diese Implementierung. Alternativ können Sie eine Implementierung der IEqualityComparer<T> generischen Schnittstelle mithilfe eines Konstruktors angeben, der einen comparer Parameter akzeptiert.
Hinweis
Wenn Sie die Größe der Auflistung abschätzen können, beseitigt die Verwendung eines Konstruktors, der die anfängliche Kapazität angibt, die Notwendigkeit, eine Reihe von Größenänderungsvorgängen auszuführen, während Elemente zum Hinzufügen von Dictionary<TKey,TValue>Elementen hinzugefügt werden.
Dieser Konstruktor ist ein O(1)-Vorgang.
Weitere Informationen
Gilt für:
Dictionary<TKey,TValue>(IDictionary<TKey,TValue>)
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
Initialisiert eine neue Instanz der Dictionary<TKey,TValue> Klasse, die Elemente enthält, die aus dem angegebenen Element IDictionary<TKey,TValue> kopiert wurden, und verwendet den Standardmäßigen Gleichheitsvergleich für den Schlüsseltyp.
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>
Die IDictionary<TKey,TValue> Elemente, deren Elemente in das neue Dictionary<TKey,TValue>kopiert werden.
Ausnahmen
dictionary ist null.
dictionary enthält mindestens einen doppelten Schlüssel.
Beispiele
Im folgenden Codebeispiel wird gezeigt, wie Sie mit dem Dictionary<TKey,TValue>(IEqualityComparer<TKey>) Konstruktor einen Dictionary<TKey,TValue> mit sortiertem Inhalt aus einem anderen Wörterbuch initialisieren. Im Codebeispiel wird ein Code SortedDictionary<TKey,TValue> erstellt und mit Daten in zufälliger Reihenfolge aufgefüllt und anschließend an den Dictionary<TKey,TValue>(IEqualityComparer<TKey>) Konstruktor übergeben SortedDictionary<TKey,TValue> und ein Dictionary<TKey,TValue> sortiertes Objekt erstellt. Dies ist nützlich, wenn Sie ein sortiertes Wörterbuch erstellen müssen, das irgendwann statisch wird; Das Kopieren der Daten von einer SortedDictionary<TKey,TValue> in eine zu einer Dictionary<TKey,TValue> verbesserten Abrufgeschwindigkeit.
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
Hinweise
Jeder Schlüssel in einem Dictionary<TKey,TValue> muss gemäß dem Standardgleichstellungsvergleich eindeutig sein. Ebenso muss jeder Schlüssel in der Quelle dictionary entsprechend dem Standardgleichgleichsvergleich eindeutig sein.
Die Anfangskapazität des neuen Dictionary<TKey,TValue> ist groß genug, um alle Elemente in dictionaryenthalten.
Dictionary<TKey,TValue> erfordert eine Gleichheitsimplementierung, um zu bestimmen, ob Schlüssel gleich sind. Dieser Konstruktor verwendet den standardmäßigen generischen Gleichheitsvergleich. EqualityComparer<T>.Default Wenn der Typ TKey die System.IEquatable<T> generische Schnittstelle implementiert, verwendet der Standardgleichheitsvergleich diese Implementierung. Alternativ können Sie eine Implementierung der IEqualityComparer<T> generischen Schnittstelle mithilfe eines Konstruktors angeben, der einen comparer Parameter akzeptiert.
Dieser Konstruktor ist ein O(n)-Vorgang, wobei n die Anzahl der Elemente in dictionary.
Weitere Informationen
Gilt für:
Dictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>)
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
Initialisiert eine neue Instanz der Dictionary<TKey,TValue> Klasse, die Elemente enthält, die aus dem angegebenen Element kopiert wurden 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>>
Die IEnumerable<T> Elemente, deren Elemente in das neue Dictionary<TKey,TValue>kopiert werden.
Ausnahmen
collection ist null.
collection enthält einen oder mehrere duplizierte Schlüssel.
Gilt für:
Dictionary<TKey,TValue>(IEqualityComparer<TKey>)
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
Initialisiert eine neue Instanz der klasse, die Dictionary<TKey,TValue> leer ist, hat die Standard-Anfangskapazität und verwendet die angegebene 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>
Die IEqualityComparer<T> Implementierung, die beim Vergleichen von Schlüsseln verwendet werden soll oder null die Standardeinstellung EqualityComparer<T> für den Schlüsseltyp verwendet werden soll.
Beispiele
Im folgenden Codebeispiel wird für die aktuelle Kultur ein Gleichheitsvergleich mit nicht beachteter Groß-/Kleinschreibung erstellt.The following code example creates a Dictionary<TKey,TValue> with a case-insensitive equality comparer for the current culture. Im Beispiel werden vier Elemente hinzugefügt, einige mit Kleinbuchstaben und einige mit Groß-/Kleinschreibung. Anschließend wird versucht, ein Element mit einem Schlüssel hinzuzufügen, der sich von einem vorhandenen Schlüssel nur im Fall unterscheidet, die resultierende Ausnahme abfangen und eine Fehlermeldung anzeigt. Schließlich zeigt das Beispiel die Elemente im Wörterbuch an.
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
Hinweise
Verwenden Sie diesen Konstruktor mit den Zeichenfolgenabgleichen, die von der StringComparer Klasse bereitgestellt werden, um Wörterbücher mit Zeichenfolgenschlüsseln ohne Groß-/Kleinschreibung zu erstellen.
Jeder Schlüssel in einem Dictionary<TKey,TValue> muss gemäß dem angegebenen Vergleich eindeutig sein.
Dictionary<TKey,TValue> erfordert eine Gleichheitsimplementierung, um zu bestimmen, ob Schlüssel gleich sind. Wenn comparer dies der Fall istnull, verwendet dieser Konstruktor den standardmäßigen generischen Gleichheitsvergleich. EqualityComparer<T>.Default Wenn der Typ TKey die System.IEquatable<T> generische Schnittstelle implementiert, verwendet der Standardgleichheitsvergleich diese Implementierung.
Hinweis
Wenn Sie die Größe der Auflistung abschätzen können, beseitigt die Verwendung eines Konstruktors, der die anfängliche Kapazität angibt, die Notwendigkeit, eine Reihe von Größenänderungsvorgängen auszuführen, während Elemente zum Hinzufügen von Dictionary<TKey,TValue>Elementen hinzugefügt werden.
Dieser Konstruktor ist ein O(1)-Vorgang.
Vorsicht
Wenn capacity die Benutzereingabe erfolgt, verwenden Sie lieber einen Konstruktor ohne Kapazitätsparameter, und lassen Sie die Größe der Sammlung ändern, wenn Elemente hinzugefügt werden. Wenn Sie einen vom Benutzer angegebenen Wert verwenden müssen, setzen Sie ihn entweder auf einen angemessenen Grenzwert (z. B. ) oder stellen Sie sicher, Math.Clamp(untrustedValue, 0, 20)dass die Elementanzahl mit dem angegebenen Wert übereinstimmt.
Weitere Informationen
Gilt für:
Dictionary<TKey,TValue>(Int32)
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
Initialisiert eine neue Instanz der klasse, die Dictionary<TKey,TValue> leer ist, die angegebene Anfangskapazität hat und den Standardgleichgleichsvergleich für den Schlüsseltyp verwendet.
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
Die anfängliche Anzahl der Elemente, die enthalten Dictionary<TKey,TValue> können.
Ausnahmen
capacity ist kleiner als 0.
Beispiele
Im folgenden Codebeispiel wird ein Wörterbuch mit einer Anfangskapazität von 4 erstellt und mit vier Einträgen aufgefüllt.
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
Hinweise
Jeder Schlüssel in einem Dictionary<TKey,TValue> muss gemäß dem Standardgleichstellungsvergleich eindeutig sein.
Die Kapazität eines Elements Dictionary<TKey,TValue> ist die Anzahl der Elemente, die Dictionary<TKey,TValue> der Größe hinzugefügt werden können, bevor eine Größenänderung erforderlich ist. Wenn Elemente zu einem Dictionary<TKey,TValue>Element hinzugefügt werden, wird die Kapazität bei Bedarf automatisch erhöht, indem das interne Array neu zugeordnet wird.
Wenn die Größe der Auflistung geschätzt werden kann, beseitigt die Angabe der anfänglichen Kapazität die Notwendigkeit, eine Reihe von Größenänderungsvorgängen auszuführen, während Elemente hinzugefügt werden Dictionary<TKey,TValue>.
Dictionary<TKey,TValue> erfordert eine Gleichheitsimplementierung, um zu bestimmen, ob Schlüssel gleich sind. Dieser Konstruktor verwendet den standardmäßigen generischen Gleichheitsvergleich. EqualityComparer<T>.Default Wenn der Typ TKey die System.IEquatable<T> generische Schnittstelle implementiert, verwendet der Standardgleichheitsvergleich diese Implementierung. Alternativ können Sie eine Implementierung der IEqualityComparer<T> generischen Schnittstelle mithilfe eines Konstruktors angeben, der einen comparer Parameter akzeptiert.
Dieser Konstruktor ist ein O(1)-Vorgang.
Vorsicht
Wenn capacity die Benutzereingabe erfolgt, verwenden Sie lieber einen Konstruktor ohne Kapazitätsparameter, und lassen Sie die Größe der Sammlung ändern, wenn Elemente hinzugefügt werden. Wenn Sie einen vom Benutzer angegebenen Wert verwenden müssen, setzen Sie ihn entweder auf einen angemessenen Grenzwert (z. B. ) oder stellen Sie sicher, Math.Clamp(untrustedValue, 0, 20)dass die Elementanzahl mit dem angegebenen Wert übereinstimmt.
Weitere Informationen
Gilt für:
Dictionary<TKey,TValue>(IDictionary<TKey,TValue>, IEqualityComparer<TKey>)
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
Initialisiert eine neue Instanz der Dictionary<TKey,TValue> Klasse, die Elemente enthält, die aus dem angegebenen Element IDictionary<TKey,TValue> kopiert wurden, und verwendet die angegebene 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>
Die IDictionary<TKey,TValue> Elemente, deren Elemente in das neue Dictionary<TKey,TValue>kopiert werden.
- comparer
- IEqualityComparer<TKey>
Die IEqualityComparer<T> Implementierung, die beim Vergleichen von Schlüsseln verwendet werden soll oder null die Standardeinstellung EqualityComparer<T> für den Schlüsseltyp verwendet werden soll.
Ausnahmen
dictionary ist null.
dictionary enthält mindestens einen doppelten Schlüssel.
Beispiele
Im folgenden Codebeispiel wird gezeigt, wie Sie mithilfe des Dictionary<TKey,TValue>(IDictionary<TKey,TValue>, IEqualityComparer<TKey>) Konstruktors einen Dictionary<TKey,TValue> mit der Groß-/Kleinschreibung sortierten Inhalt aus einem anderen Wörterbuch initialisieren. Im Codebeispiel wird ein SortedDictionary<TKey,TValue> Mit einem Vergleich ohne Groß-/Kleinschreibung erstellt und mit Daten in zufälliger Reihenfolge aufgefüllt. Anschließend wird der SortedDictionary<TKey,TValue> Konstruktor an den Dictionary<TKey,TValue>(IDictionary<TKey,TValue>, IEqualityComparer<TKey>) Konstruktor übergeben, zusammen mit einem Gleichheitsvergleich ohne Groß-/Kleinschreibung, wobei ein Dictionary<TKey,TValue> sortierter Vergleich zwischen Groß- und Kleinschreibung erstellt wird. Dies ist nützlich, wenn Sie ein sortiertes Wörterbuch erstellen müssen, das irgendwann statisch wird; Das Kopieren der Daten von einer SortedDictionary<TKey,TValue> in eine zu einer Dictionary<TKey,TValue> verbesserten Abrufgeschwindigkeit.
Hinweis
Wenn Sie ein neues Wörterbuch mit einem Vergleich ohne Groß-/Kleinschreibung erstellen und es mit Einträgen aus einem Wörterbuch auffüllen, bei dem ein Vergleich zwischen Groß- und Kleinschreibung verwendet wird, tritt eine Ausnahme auf, wenn das Eingabewörterbuch Schlüssel aufweist, die sich nur nach Groß-/Kleinschreibung unterscheiden.
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
Hinweise
Verwenden Sie diesen Konstruktor mit den Zeichenfolgenabgleichen, die von der StringComparer Klasse bereitgestellt werden, um Wörterbücher mit Zeichenfolgenschlüsseln ohne Groß-/Kleinschreibung zu erstellen.
Jeder Schlüssel in einem Dictionary<TKey,TValue> muss gemäß dem angegebenen Vergleich eindeutig sein. Ebenso muss jeder Schlüssel in der Quelle dictionary entsprechend dem angegebenen Vergleich eindeutig sein.
Hinweis
Beispielsweise können doppelte Schlüssel auftreten, wenn comparer es sich um einen der von der StringComparer Klasse bereitgestellten Zeichenfolgenabgleiche mit Groß-/Kleinschreibung handelt und dictionary keine Groß-/Kleinschreibung verwendet.
Die Anfangskapazität des neuen Dictionary<TKey,TValue> ist groß genug, um alle Elemente in dictionaryenthalten.
Dictionary<TKey,TValue> erfordert eine Gleichheitsimplementierung, um zu bestimmen, ob Schlüssel gleich sind. Wenn comparer dies der Fall istnull, verwendet dieser Konstruktor den standardmäßigen generischen Gleichheitsvergleich. EqualityComparer<T>.Default Wenn der Typ TKey die System.IEquatable<T> generische Schnittstelle implementiert, verwendet der Standardgleichheitsvergleich diese Implementierung.
Dieser Konstruktor ist ein O(n)-Vorgang. n Dabei handelt es sich um die Anzahl der Elemente in dictionary.
Weitere Informationen
Gilt für:
Dictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>, IEqualityComparer<TKey>)
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
Initialisiert eine neue Instanz der Dictionary<TKey,TValue> Klasse, die Elemente enthält, die aus dem angegebenen Element IEnumerable<T> kopiert wurden, und verwendet die angegebene 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>>
Die IEnumerable<T> Elemente, deren Elemente in das neue Dictionary<TKey,TValue>kopiert werden.
- comparer
- IEqualityComparer<TKey>
Die IEqualityComparer<T> Implementierung, die beim Vergleichen von Schlüsseln verwendet werden soll oder null die Standardeinstellung EqualityComparer<T> für den Schlüsseltyp verwendet werden soll.
Ausnahmen
collection ist null.
collection enthält einen oder mehrere duplizierte Schlüssel.
Gilt für:
Dictionary<TKey,TValue>(Int32, IEqualityComparer<TKey>)
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
Initialisiert eine neue Instanz der klasse, die Dictionary<TKey,TValue> leer ist, die angegebene Anfangskapazität hat und verwendet die angegebene 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
Die anfängliche Anzahl der Elemente, die enthalten Dictionary<TKey,TValue> können.
- comparer
- IEqualityComparer<TKey>
Die IEqualityComparer<T> Implementierung, die beim Vergleichen von Schlüsseln verwendet werden soll oder null die Standardeinstellung EqualityComparer<T> für den Schlüsseltyp verwendet werden soll.
Ausnahmen
capacity ist kleiner als 0.
Beispiele
Im folgenden Codebeispiel wird eine Dictionary<TKey,TValue> Mit einer Anfangskapazität von 5 und ein Gleichheitsvergleich mit Groß-/Kleinschreibung für die aktuelle Kultur erstellt. Im Beispiel werden vier Elemente hinzugefügt, einige mit Kleinbuchstaben und einige mit Groß-/Kleinschreibung. Anschließend wird versucht, ein Element mit einem Schlüssel hinzuzufügen, der sich von einem vorhandenen Schlüssel nur im Fall unterscheidet, die resultierende Ausnahme abfangen und eine Fehlermeldung anzeigt. Schließlich zeigt das Beispiel die Elemente im Wörterbuch an.
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
Hinweise
Verwenden Sie diesen Konstruktor mit den Zeichenfolgenabgleichen, die von der StringComparer Klasse bereitgestellt werden, um Wörterbücher mit Zeichenfolgenschlüsseln ohne Groß-/Kleinschreibung zu erstellen.
Jeder Schlüssel in einem Dictionary<TKey,TValue> muss gemäß dem angegebenen Vergleich eindeutig sein.
Die Kapazität eines Elements Dictionary<TKey,TValue> ist die Anzahl der Elemente, die Dictionary<TKey,TValue> der Größe hinzugefügt werden können, bevor eine Größenänderung erforderlich ist. Wenn Elemente zu einem Dictionary<TKey,TValue>Element hinzugefügt werden, wird die Kapazität bei Bedarf automatisch erhöht, indem das interne Array neu zugeordnet wird.
Wenn die Größe der Auflistung geschätzt werden kann, beseitigt die Angabe der anfänglichen Kapazität die Notwendigkeit, eine Reihe von Größenänderungsvorgängen auszuführen, während Elemente hinzugefügt werden Dictionary<TKey,TValue>.
Dictionary<TKey,TValue> erfordert eine Gleichheitsimplementierung, um zu bestimmen, ob Schlüssel gleich sind. Wenn comparer dies der Fall istnull, verwendet dieser Konstruktor den standardmäßigen generischen Gleichheitsvergleich. EqualityComparer<T>.Default Wenn der Typ TKey die System.IEquatable<T> generische Schnittstelle implementiert, verwendet der Standardgleichheitsvergleich diese Implementierung.
Dieser Konstruktor ist ein O(1)-Vorgang.
Weitere Informationen
Gilt für:
Dictionary<TKey,TValue>(SerializationInfo, StreamingContext)
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
- Quelle:
- Dictionary.cs
Achtung
This API supports obsolete formatter-based serialization. It should not be called or extended by application code.
Initialisiert eine neue Instanz der Dictionary<TKey,TValue> Klasse mit serialisierten Daten.
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)
Parameter
- info
- SerializationInfo
Ein SerializationInfo Objekt, das die zum Serialisieren der Dictionary<TKey,TValue>Erforderlichen Informationen enthält.
- context
- StreamingContext
Eine StreamingContext Struktur, die die Quelle und das Ziel des serialisierten Datenstroms enthält, der der Dictionary<TKey,TValue>Datei zugeordnet ist.
- Attribute
Hinweise
Dieser Konstruktor wird während der Deserialisierung aufgerufen, um ein objekt zu rekonstituieren, das über einen Datenstrom übertragen wird. Weitere Informationen finden Sie unter XML- und SOAP-Serialisierung.