Dictionary<TKey,TValue> Constructors
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Initialiseert een nieuw exemplaar van de Dictionary<TKey,TValue> klasse.
Overloads
| Name | Description |
|---|---|
| Dictionary<TKey,TValue>() |
Initialiseert een nieuw exemplaar van de Dictionary<TKey,TValue> klasse die leeg is, heeft de standaardinitiële capaciteit en gebruikt de standaard gelijkheidsgelijker voor het sleuteltype. |
| Dictionary<TKey,TValue>(IDictionary<TKey,TValue>) |
Initialiseert een nieuw exemplaar van de Dictionary<TKey,TValue> klasse die elementen bevat die zijn gekopieerd uit de opgegeven IDictionary<TKey,TValue> en maakt gebruik van de standaard gelijkheidsgelijker voor het sleuteltype. |
| Dictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>) |
Initialiseert een nieuw exemplaar van de Dictionary<TKey,TValue> klasse die elementen bevat die zijn gekopieerd uit de opgegeven IEnumerable<T>. |
| Dictionary<TKey,TValue>(IEqualityComparer<TKey>) |
Initialiseert een nieuw exemplaar van de Dictionary<TKey,TValue> klasse die leeg is, heeft de standaardinitiële capaciteit en gebruikt de opgegeven IEqualityComparer<T>. |
| Dictionary<TKey,TValue>(Int32) |
Initialiseert een nieuw exemplaar van de Dictionary<TKey,TValue> klasse die leeg is, heeft de opgegeven initiële capaciteit en gebruikt de standaard gelijkheidsgelijker voor het sleuteltype. |
| Dictionary<TKey,TValue>(IDictionary<TKey,TValue>, IEqualityComparer<TKey>) |
Initialiseert een nieuw exemplaar van de Dictionary<TKey,TValue> klasse die elementen bevat die zijn gekopieerd uit de opgegeven IDictionary<TKey,TValue> en maakt gebruik van de opgegeven IEqualityComparer<T>. |
| Dictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>, IEqualityComparer<TKey>) |
Initialiseert een nieuw exemplaar van de Dictionary<TKey,TValue> klasse die elementen bevat die zijn gekopieerd uit de opgegeven IEnumerable<T> en maakt gebruik van de opgegeven IEqualityComparer<T>. |
| Dictionary<TKey,TValue>(Int32, IEqualityComparer<TKey>) |
Initialiseert een nieuw exemplaar van de Dictionary<TKey,TValue> klasse die leeg is, heeft de opgegeven initiële capaciteit en gebruikt de opgegeven IEqualityComparer<T>. |
| Dictionary<TKey,TValue>(SerializationInfo, StreamingContext) |
Initialiseert een nieuw exemplaar van de Dictionary<TKey,TValue> klasse met geserialiseerde gegevens. |
Dictionary<TKey,TValue>()
Initialiseert een nieuw exemplaar van de Dictionary<TKey,TValue> klasse die leeg is, heeft de standaardinitiële capaciteit en gebruikt de standaard gelijkheidsgelijker voor het sleuteltype.
public:
Dictionary();
public Dictionary();
Public Sub New ()
Voorbeelden
In het volgende codevoorbeeld wordt een lege Dictionary<TKey,TValue> tekenreeks met tekenreekssleutels gemaakt en wordt de Add methode gebruikt om enkele elementen toe te voegen. In het voorbeeld ziet u dat de Add methode een ArgumentException dubbele sleutel genereert bij het toevoegen van een dubbele sleutel.
Dit codevoorbeeld maakt deel uit van een groter voorbeeld voor de Dictionary<TKey,TValue> klasse.
// 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
Opmerkingen
Elke sleutel in een Dictionary<TKey,TValue> moet uniek zijn volgens de standaard gelijkheidsgelijker.
Dictionary<TKey,TValue> vereist een gelijkheidsuitvoering om te bepalen of sleutels gelijk zijn. Deze constructor maakt gebruik van de standaard algemene gelijkheids comparer, EqualityComparer<T>.Default. Als het type TKey de System.IEquatable<T> algemene interface implementeert, gebruikt de standaard gelijkheidsgelijker die implementatie. U kunt ook een implementatie van de IEqualityComparer<T> algemene interface opgeven met behulp van een constructor die een comparer parameter accepteert.
Note
Als u de grootte van de verzameling kunt schatten met behulp van een constructor die de initiële capaciteit aangeeft, hoeft u geen bewerkingen voor het wijzigen van de grootte uit te voeren terwijl u elementen aan de Dictionary<TKey,TValue>verzameling toevoegt.
Deze constructor is een O(1)-bewerking.
Zie ook
Van toepassing op
Dictionary<TKey,TValue>(IDictionary<TKey,TValue>)
Initialiseert een nieuw exemplaar van de Dictionary<TKey,TValue> klasse die elementen bevat die zijn gekopieerd uit de opgegeven IDictionary<TKey,TValue> en maakt gebruik van de standaard gelijkheidsgelijker voor het sleuteltype.
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))
Parameters
- dictionary
- IDictionary<TKey,TValue>
De IDictionary<TKey,TValue> elementen waarvan de elementen naar het nieuwe Dictionary<TKey,TValue>worden gekopieerd.
Uitzonderingen
dictionary is null.
dictionary bevat een of meer dubbele sleutels.
Voorbeelden
In het volgende codevoorbeeld ziet u hoe u de Dictionary<TKey,TValue>(IEqualityComparer<TKey>) constructor gebruikt om een met gesorteerde inhoud uit een Dictionary<TKey,TValue> andere woordenlijst te initialiseren. Het codevoorbeeld maakt een SortedDictionary<TKey,TValue> en vult deze in willekeurige volgorde met gegevens en geeft vervolgens de SortedDictionary<TKey,TValue> constructor door aan de Dictionary<TKey,TValue>(IEqualityComparer<TKey>) constructor, waardoor een Dictionary<TKey,TValue> die wordt gesorteerd. Dit is handig als u een gesorteerde woordenlijst moet maken die op een bepaald moment statisch wordt; Het kopiëren van de gegevens van een SortedDictionary<TKey,TValue> naar een Dictionary<TKey,TValue> verbetert de ophaalsnelheid.
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
Opmerkingen
Elke sleutel in een Dictionary<TKey,TValue> moet uniek zijn op basis van de standaard gelijkheids comparer. Op dezelfde manier moet elke sleutel in de bron dictionary ook uniek zijn volgens de standaard gelijkheids comparer.
De initiële capaciteit van de nieuwe Dictionary<TKey,TValue> is groot genoeg om alle elementen in dictionaryte bevatten.
Dictionary<TKey,TValue> vereist een gelijkheidsuitvoering om te bepalen of sleutels gelijk zijn. Deze constructor maakt gebruik van de standaard algemene gelijkheids comparer, EqualityComparer<T>.Default. Als het type TKey de System.IEquatable<T> algemene interface implementeert, gebruikt de standaard gelijkheidsgelijker die implementatie. U kunt ook een implementatie van de IEqualityComparer<T> algemene interface opgeven met behulp van een constructor die een comparer parameter accepteert.
Deze constructor is een O(n)-bewerking, waarbij n het aantal elementen in dictionaryis.
Zie ook
Van toepassing op
Dictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>)
Initialiseert een nieuw exemplaar van de Dictionary<TKey,TValue> klasse die elementen bevat die zijn gekopieerd uit de opgegeven 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)))
Parameters
- collection
- IEnumerable<KeyValuePair<TKey,TValue>>
De IEnumerable<T> elementen waarvan de elementen naar het nieuwe Dictionary<TKey,TValue>worden gekopieerd.
Uitzonderingen
collection is null.
collection bevat een of meer dubbele sleutels.
Van toepassing op
Dictionary<TKey,TValue>(IEqualityComparer<TKey>)
Initialiseert een nieuw exemplaar van de Dictionary<TKey,TValue> klasse die leeg is, heeft de standaardinitiële capaciteit en gebruikt de opgegeven IEqualityComparer<T>.
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))
Parameters
- comparer
- IEqualityComparer<TKey>
De IEqualityComparer<T> implementatie die moet worden gebruikt bij het vergelijken van sleutels of null voor het gebruik van de standaardwaarde EqualityComparer<T> voor het type sleutel.
Voorbeelden
In het volgende codevoorbeeld wordt een Dictionary<TKey,TValue> met een niet-hoofdlettergevoelige gelijkheidsvergelijker voor de huidige cultuur gemaakt. In het voorbeeld worden vier elementen toegevoegd, sommige met kleine letters en sommige met hoofdletters. Het voorbeeld probeert vervolgens een element toe te voegen met een sleutel die alleen per geval verschilt van een bestaande sleutel, de resulterende uitzondering onderschept en een foutbericht weergeeft. Ten slotte worden in het voorbeeld de elementen in de woordenlijst weergegeven.
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
Opmerkingen
Gebruik deze constructor met de hoofdlettergevoelige tekenreeksvergelijkers van de StringComparer klasse om woordenlijsten te maken met niet-hoofdlettergevoelige tekenreekssleutels.
Elke sleutel in een Dictionary<TKey,TValue> moet uniek zijn volgens de opgegeven vergelijkingsfunctie.
Dictionary<TKey,TValue> vereist een gelijkheidsuitvoering om te bepalen of sleutels gelijk zijn. Als comparer dat het is null, gebruikt deze constructor de standaard algemene gelijkheidsgelijker, EqualityComparer<T>.Default. Als het type TKey de System.IEquatable<T> algemene interface implementeert, gebruikt de standaard gelijkheidsgelijker die implementatie.
Note
Als u de grootte van de verzameling kunt schatten met behulp van een constructor die de initiële capaciteit aangeeft, hoeft u geen bewerkingen voor het wijzigen van de grootte uit te voeren terwijl u elementen aan de Dictionary<TKey,TValue>verzameling toevoegt.
Deze constructor is een O(1)-bewerking.
Caution
Als capacity deze afkomstig is van gebruikersinvoer, geeft u de voorkeur aan het gebruik van een constructor zonder capaciteitsparameter en kunt u de grootte van de verzameling wijzigen als elementen worden toegevoegd. Als u een door de gebruiker opgegeven waarde moet gebruiken, kunt u deze vastzetten op een redelijke limiet (bijvoorbeeld Math.Clamp(untrustedValue, 0, 20)) of controleren of het aantal elementen overeenkomt met de opgegeven waarde.
Zie ook
Van toepassing op
Dictionary<TKey,TValue>(Int32)
Initialiseert een nieuw exemplaar van de Dictionary<TKey,TValue> klasse die leeg is, heeft de opgegeven initiële capaciteit en gebruikt de standaard gelijkheidsgelijker voor het sleuteltype.
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)
Parameters
- capacity
- Int32
Het eerste aantal elementen dat de Dictionary<TKey,TValue> elementen kunnen bevatten.
Uitzonderingen
capacity is kleiner dan 0.
Voorbeelden
In het volgende codevoorbeeld wordt een woordenlijst gemaakt met een initiële capaciteit van 4 en wordt deze gevuld met vier vermeldingen.
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
Opmerkingen
Elke sleutel in een Dictionary<TKey,TValue> moet uniek zijn volgens de standaard gelijkheidsgelijker.
De capaciteit van een Dictionary<TKey,TValue> is het aantal elementen dat kan worden toegevoegd aan de Dictionary<TKey,TValue> voordat het formaat wordt aangepast. Als er elementen aan een Dictionary<TKey,TValue>worden toegevoegd, wordt de capaciteit automatisch verhoogd zoals vereist door de interne matrix opnieuw te verplaatsen.
Als de grootte van de verzameling kan worden geschat, hoeft u bij het opgeven van de initiële capaciteit niet meer een aantal groottebewerkingen uit te voeren terwijl er elementen aan de Dictionary<TKey,TValue>verzameling worden toegevoegd.
Dictionary<TKey,TValue> vereist een gelijkheidsuitvoering om te bepalen of sleutels gelijk zijn. Deze constructor maakt gebruik van de standaard algemene gelijkheids comparer, EqualityComparer<T>.Default. Als het type TKey de System.IEquatable<T> algemene interface implementeert, gebruikt de standaard gelijkheidsgelijker die implementatie. U kunt ook een implementatie van de IEqualityComparer<T> algemene interface opgeven met behulp van een constructor die een comparer parameter accepteert.
Deze constructor is een O(1)-bewerking.
Caution
Als capacity deze afkomstig is van gebruikersinvoer, geeft u de voorkeur aan het gebruik van een constructor zonder capaciteitsparameter en kunt u de grootte van de verzameling wijzigen als elementen worden toegevoegd. Als u een door de gebruiker opgegeven waarde moet gebruiken, kunt u deze vastzetten op een redelijke limiet (bijvoorbeeld Math.Clamp(untrustedValue, 0, 20)) of controleren of het aantal elementen overeenkomt met de opgegeven waarde.
Zie ook
Van toepassing op
Dictionary<TKey,TValue>(IDictionary<TKey,TValue>, IEqualityComparer<TKey>)
Initialiseert een nieuw exemplaar van de Dictionary<TKey,TValue> klasse die elementen bevat die zijn gekopieerd uit de opgegeven IDictionary<TKey,TValue> en maakt gebruik van de opgegeven 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);
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))
Parameters
- dictionary
- IDictionary<TKey,TValue>
De IDictionary<TKey,TValue> elementen waarvan de elementen naar het nieuwe Dictionary<TKey,TValue>worden gekopieerd.
- comparer
- IEqualityComparer<TKey>
De IEqualityComparer<T> implementatie die moet worden gebruikt bij het vergelijken van sleutels of null voor het gebruik van de standaardwaarde EqualityComparer<T> voor het type sleutel.
Uitzonderingen
dictionary is null.
dictionary bevat een of meer dubbele sleutels.
Voorbeelden
In het volgende codevoorbeeld ziet u hoe u de Dictionary<TKey,TValue>(IDictionary<TKey,TValue>, IEqualityComparer<TKey>) constructor gebruikt om een Dictionary<TKey,TValue> met hoofdlettergevoelige gesorteerde inhoud uit een andere woordenlijst te initialiseren. In het codevoorbeeld wordt een SortedDictionary<TKey,TValue> met een niet-hoofdlettergevoelige vergelijking gemaakt en gevuld met gegevens in willekeurige volgorde. Vervolgens wordt de SortedDictionary<TKey,TValue> constructor doorgegeven aan de Dictionary<TKey,TValue>(IDictionary<TKey,TValue>, IEqualityComparer<TKey>) constructor, samen met een niet-hoofdlettergevoelige gelijkheidsvergelijkingsvergelijker, waardoor een Dictionary<TKey,TValue> vergelijking wordt gemaakt die wordt gesorteerd. Dit is handig als u een gesorteerde woordenlijst moet maken die op een bepaald moment statisch wordt; Het kopiëren van de gegevens van een SortedDictionary<TKey,TValue> naar een Dictionary<TKey,TValue> verbetert de ophaalsnelheid.
Note
Wanneer u een nieuwe woordenlijst maakt met een niet-hoofdlettergevoelige vergelijking en deze vult met vermeldingen uit een woordenlijst die gebruikmaakt van een hoofdlettergevoelige vergelijking, zoals in dit voorbeeld, treedt er een uitzondering op als de invoerwoordenlijst sleutels bevat die alleen per hoofdletter verschillen.
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
Opmerkingen
Gebruik deze constructor met de hoofdlettergevoelige tekenreeksvergelijkers van de StringComparer klasse om woordenlijsten te maken met niet-hoofdlettergevoelige tekenreekssleutels.
Elke sleutel in een Dictionary<TKey,TValue> moet uniek zijn volgens de opgegeven vergelijkingsfunctie. Op dezelfde manier moet elke sleutel in de bron dictionary ook uniek zijn volgens de opgegeven vergelijkingsfunctie.
Note
Dubbele sleutels kunnen bijvoorbeeld optreden als comparer een van de hoofdlettergevoelige tekenreeksvergelijkers van de StringComparer klasse is en dictionary geen hoofdlettergevoelige vergelijkingssleutel gebruikt.
De initiële capaciteit van de nieuwe Dictionary<TKey,TValue> is groot genoeg om alle elementen in dictionaryte bevatten.
Dictionary<TKey,TValue> vereist een gelijkheidsuitvoering om te bepalen of sleutels gelijk zijn. Als comparer dat het is null, gebruikt deze constructor de standaard algemene gelijkheidsgelijker, EqualityComparer<T>.Default. Als het type TKey de System.IEquatable<T> algemene interface implementeert, gebruikt de standaard gelijkheidsgelijker die implementatie.
Deze constructor is een O(n)-bewerking, waarbij n het aantal elementen in dictionary.
Zie ook
Van toepassing op
Dictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>, IEqualityComparer<TKey>)
Initialiseert een nieuw exemplaar van de Dictionary<TKey,TValue> klasse die elementen bevat die zijn gekopieerd uit de opgegeven IEnumerable<T> en maakt gebruik van de opgegeven 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);
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))
Parameters
- collection
- IEnumerable<KeyValuePair<TKey,TValue>>
De IEnumerable<T> elementen waarvan de elementen naar het nieuwe Dictionary<TKey,TValue>worden gekopieerd.
- comparer
- IEqualityComparer<TKey>
De IEqualityComparer<T> implementatie die moet worden gebruikt bij het vergelijken van sleutels of null voor het gebruik van de standaardwaarde EqualityComparer<T> voor het type sleutel.
Uitzonderingen
collection is null.
collection bevat een of meer dubbele sleutels.
Van toepassing op
Dictionary<TKey,TValue>(Int32, IEqualityComparer<TKey>)
Initialiseert een nieuw exemplaar van de Dictionary<TKey,TValue> klasse die leeg is, heeft de opgegeven initiële capaciteit en gebruikt de opgegeven IEqualityComparer<T>.
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))
Parameters
- capacity
- Int32
Het eerste aantal elementen dat de Dictionary<TKey,TValue> elementen kunnen bevatten.
- comparer
- IEqualityComparer<TKey>
De IEqualityComparer<T> implementatie die moet worden gebruikt bij het vergelijken van sleutels of null voor het gebruik van de standaardwaarde EqualityComparer<T> voor het type sleutel.
Uitzonderingen
capacity is kleiner dan 0.
Voorbeelden
In het volgende codevoorbeeld wordt een Dictionary<TKey,TValue> met een initiële capaciteit van 5 en een niet-hoofdlettergevoelige gelijkheidsvergelijker voor de huidige cultuur gemaakt. In het voorbeeld worden vier elementen toegevoegd, sommige met kleine letters en sommige met hoofdletters. Het voorbeeld probeert vervolgens een element toe te voegen met een sleutel die alleen per geval verschilt van een bestaande sleutel, de resulterende uitzondering onderschept en een foutbericht weergeeft. Ten slotte worden in het voorbeeld de elementen in de woordenlijst weergegeven.
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
Opmerkingen
Gebruik deze constructor met de hoofdlettergevoelige tekenreeksvergelijkers van de StringComparer klasse om woordenlijsten te maken met niet-hoofdlettergevoelige tekenreekssleutels.
Elke sleutel in een Dictionary<TKey,TValue> moet uniek zijn volgens de opgegeven vergelijkingsfunctie.
De capaciteit van een Dictionary<TKey,TValue> is het aantal elementen dat kan worden toegevoegd aan de Dictionary<TKey,TValue> voordat het formaat wordt aangepast. Als er elementen aan een Dictionary<TKey,TValue>worden toegevoegd, wordt de capaciteit automatisch verhoogd zoals vereist door de interne matrix opnieuw te verplaatsen.
Als de grootte van de verzameling kan worden geschat, hoeft u bij het opgeven van de initiële capaciteit niet meer een aantal groottebewerkingen uit te voeren terwijl er elementen aan de Dictionary<TKey,TValue>verzameling worden toegevoegd.
Dictionary<TKey,TValue> vereist een gelijkheidsuitvoering om te bepalen of sleutels gelijk zijn. Als comparer dat het is null, gebruikt deze constructor de standaard algemene gelijkheidsgelijker, EqualityComparer<T>.Default. Als het type TKey de System.IEquatable<T> algemene interface implementeert, gebruikt de standaard gelijkheidsgelijker die implementatie.
Deze constructor is een O(1)-bewerking.
Zie ook
Van toepassing op
Dictionary<TKey,TValue>(SerializationInfo, StreamingContext)
Initialiseert een nieuw exemplaar van de Dictionary<TKey,TValue> klasse met geserialiseerde gegevens.
protected:
Dictionary(System::Runtime::Serialization::SerializationInfo ^ info, System::Runtime::Serialization::StreamingContext context);
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>
Protected Sub New (info As SerializationInfo, context As StreamingContext)
Parameters
- info
- SerializationInfo
Een SerializationInfo object met de informatie die nodig is voor het serialiseren van de Dictionary<TKey,TValue>.
- context
- StreamingContext
Een StreamingContext structuur met de bron en het doel van de geserialiseerde stroom die is gekoppeld aan de Dictionary<TKey,TValue>.
Opmerkingen
Deze constructor wordt aangeroepen tijdens deserialisatie om een object te reconstitueren dat via een stroom wordt verzonden. Zie XML- en SOAP-serialisatie voor meer informatie.