KeyValuePair<TKey,TValue> Struktura

Definice

Definuje pár klíč/hodnota, který lze nastavit nebo načíst.

generic <typename TKey, typename TValue>
public value class KeyValuePair
public struct KeyValuePair<TKey,TValue>
public readonly 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.

TValue

Typ hodnoty

Dědičnost
KeyValuePair<TKey,TValue>
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.

Tento kód je součástí většího příkladu Dictionary<TKey,TValue> , který je k dispozici pro třídu.

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

Vlastnost Dictionary<TKey,TValue>.Enumerator.Current vrátí instanci tohoto typu.

Příkaz foreach jazyka C# (for each v jazyce C++, For Each v jazyce Visual Basic) vrátí objekt typu elementů v kolekci. Vzhledem k tomu, že každý prvek kolekce založené na IDictionary<TKey,TValue> je pár klíč/hodnota, typ elementu není typem klíče ani typem hodnoty. Místo toho je KeyValuePair<TKey,TValue>typ elementu . Příklad:

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

Příkaz foreach je obálka kolem enumerátoru, který umožňuje pouze čtení z kolekce, nikoli zápis do.

Konstruktory

KeyValuePair<TKey,TValue>(TKey, TValue)

Inicializuje novou instanci KeyValuePair<TKey,TValue> struktury pomocí zadaného klíče a hodnoty.

Vlastnosti

Key

Získá klíč ve dvojici klíč/hodnota.

Value

Získá hodnotu v páru klíč/hodnota.

Metody

Deconstruct(TKey, TValue)

Dekonstruuje aktuální KeyValuePair<TKey,TValue>.

ToString()

Vrátí řetězcovou reprezentaci KeyValuePair<TKey,TValue>, pomocí řetězcových reprezentací klíče a hodnoty.

Platí pro

Viz také