KeyValuePair<TKey,TValue> Struktur
Definition
Definiert ein Schlüssel-Wert-Paar, das festgelegt oder abgerufen werden kann.Defines a key/value pair that can be set or retrieved.
generic <typename TKey, typename TValue>
public value class KeyValuePair
public struct KeyValuePair<TKey,TValue>
[System.Serializable]
public struct KeyValuePair<TKey,TValue>
type KeyValuePair<'Key, 'Value> = struct
[<System.Serializable>]
type KeyValuePair<'Key, 'Value> = struct
Public Structure KeyValuePair(Of TKey, TValue)
Typparameter
- TKey
Der Typ des Schlüssels.The type of the key.
- TValue
Der Typ des Werts.The type of the value.
- Vererbung
- Attribute
Beispiele
Im folgenden Codebeispiel wird gezeigt, wie die Schlüssel und Werte in einem Wörterbuch mithilfe der- KeyValuePair<TKey,TValue> Struktur aufgelistet werden.The following code example shows how to enumerate the keys and values in a dictionary, using the KeyValuePair<TKey,TValue> structure.
Dieser Code ist Teil eines größeren Beispiels, das für die-Klasse bereitgestellt wird Dictionary<TKey,TValue> .This code is part of a larger example provided for the Dictionary<TKey,TValue> class.
// When you use foreach to enumerate dictionary elements,
// the elements are retrieved as KeyValuePair objects.
Console::WriteLine();
for each( KeyValuePair<String^, String^> kvp in openWith )
{
Console::WriteLine("Key = {0}, Value = {1}",
kvp.Key, kvp.Value);
}
// When you use foreach to enumerate dictionary elements,
// the elements are retrieved as KeyValuePair objects.
Console.WriteLine();
foreach( KeyValuePair<string, string> kvp in openWith )
{
Console.WriteLine("Key = {0}, Value = {1}",
kvp.Key, kvp.Value);
}
' When you use foreach to enumerate dictionary elements,
' the elements are retrieved as KeyValuePair objects.
Console.WriteLine()
For Each kvp As KeyValuePair(Of String, String) In openWith
Console.WriteLine("Key = {0}, Value = {1}", _
kvp.Key, kvp.Value)
Next kvp
Hinweise
Die- Dictionary<TKey,TValue>.Enumerator.Current Eigenschaft gibt eine Instanz dieses Typs zurück.The Dictionary<TKey,TValue>.Enumerator.Current property returns an instance of this type.
Die- foreach
Anweisung der c#-Sprache ( for each
in C++, For Each
in Visual Basic) gibt ein Objekt des Typs der Elemente in der Auflistung zurück.The foreach
statement of the C# language (for each
in C++, For Each
in Visual Basic) returns an object of the type of the elements in the collection. Da jedes Element einer Auflistung, das auf basiert IDictionary<TKey,TValue> , ein Schlüssel-Wert-Paar ist, ist der Elementtyp nicht der Typ des Schlüssels oder der Typ des Werts.Since each element of a collection based on IDictionary<TKey,TValue> is a key/value pair, the element type is not the type of the key or the type of the value. Stattdessen ist der Elementtyp KeyValuePair<TKey,TValue> .Instead, the element type is KeyValuePair<TKey,TValue>. Zum Beispiel:For example:
for each(KeyValuePair<String^, String^> kvp in myDictionary)
{
Console::WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}
foreach( KeyValuePair<string, string> kvp in myDictionary )
{
Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}
For Each kvp As KeyValuePair(Of String, String) In myDictionary
Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value)
Next kvp
Bei der- foreach
Anweisung handelt es sich um einen Wrapper um den Enumerator, der nur das Lesen von und das Schreiben in die Auflistung ermöglicht.The foreach
statement is a wrapper around the enumerator, which allows only reading from, not writing to, the collection.
Konstruktoren
KeyValuePair<TKey,TValue>(TKey, TValue) |
Initialisiert eine neue Instanz der KeyValuePair<TKey,TValue>-Struktur mit dem angegebenen Schlüssel und Wert.Initializes a new instance of the KeyValuePair<TKey,TValue> structure with the specified key and value. |
Eigenschaften
Key |
Ruft den Schlüssel im Schlüssel-Wert-Paar ab.Gets the key in the key/value pair. |
Value |
Ruft den Wert im Schlüssel-Wert-Paar ab.Gets the value in the key/value pair. |
Methoden
Deconstruct(TKey, TValue) |
Dekonstruiert die aktuelle KeyValuePair<TKey,TValue>-Struktur.Deconstructs the current KeyValuePair<TKey,TValue>. |
ToString() |
Gibt eine Zeichenfolgenentsprechung des KeyValuePair<TKey,TValue> zurück, wobei die Zeichenfolgenentsprechungen des Schlüssels und des Werts verwendet werden.Returns a string representation of the KeyValuePair<TKey,TValue>, using the string representations of the key and value. |