KeyValuePair<TKey,TValue> Struktura
Definice
Definuje pár klíč/hodnota, který lze nastavit nebo načíst.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)
Parametry typu
- TKey
Typ klíče.The type of the key.
- TValue
Typ hodnotyThe type of the value.
- Dědičnost
- Atributy
Příklady
Následující příklad kódu ukazuje, jak vytvořit výčet klíčů a hodnot ve slovníku pomocí KeyValuePair<TKey,TValue> struktury.The following code example shows how to enumerate the keys and values in a dictionary, using the KeyValuePair<TKey,TValue> structure.
Tento kód je součástí většího příkladu, který je k dispozici pro Dictionary<TKey,TValue> třídu.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
Poznámky
Dictionary<TKey,TValue>.Enumerator.CurrentVlastnost vrací instanci tohoto typu.The Dictionary<TKey,TValue>.Enumerator.Current property returns an instance of this type.
foreach
Příkaz jazyka C# ( for each
v jazyce C++, For Each
v Visual Basic) vrací objekt typu prvků v kolekci.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. Vzhledem k tomu, že každý prvek kolekce na základě IDictionary<TKey,TValue> je dvojice klíč/hodnota, typ prvku není typ klíče nebo typ hodnoty.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. Místo toho je typ prvku KeyValuePair<TKey,TValue> .Instead, the element type is KeyValuePair<TKey,TValue>. Příklad: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
foreach
Příkaz je Obálka kolem čítače, který umožňuje čtení z kolekce, nikoli zápis do kolekce.The foreach
statement is a wrapper around the enumerator, which allows only reading from, not writing to, the collection.
Konstruktory
KeyValuePair<TKey,TValue>(TKey, TValue) |
Inicializuje novou instanci KeyValuePair<TKey,TValue> struktury se zadaným klíčem a hodnotou.Initializes a new instance of the KeyValuePair<TKey,TValue> structure with the specified key and value. |
Vlastnosti
Key |
Získá klíč v páru klíč/hodnota.Gets the key in the key/value pair. |
Value |
Získá hodnotu v páru klíč/hodnota.Gets the value in the key/value pair. |
Metody
Deconstruct(TKey, TValue) |
Dekonstruuje aktuální KeyValuePair<TKey,TValue> .Deconstructs the current KeyValuePair<TKey,TValue>. |
ToString() |
Vrátí řetězcovou reprezentaci KeyValuePair<TKey,TValue> hodnoty pomocí řetězcových reprezentací klíče a hodnoty.Returns a string representation of the KeyValuePair<TKey,TValue>, using the string representations of the key and value. |