KeyValuePair<TKey,TValue> Struktura

Definicja

Definiuje parę klucz/wartość, którą można ustawić lub pobrać.

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 klucza.

TValue

Typ wartości.

Dziedziczenie
KeyValuePair<TKey,TValue>
Atrybuty

Przykłady

Poniższy przykład kodu przedstawia sposób wyliczania kluczy i wartości w słowniku KeyValuePair<TKey,TValue> przy użyciu struktury.

Ten kod jest częścią większego przykładu udostępnionego Dictionary<TKey,TValue> dla klasy.

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

Uwagi

Właściwość Dictionary<TKey,TValue>.Enumerator.Current zwraca wystąpienie tego typu.

Instrukcja foreach języka C# (for each w języku C++ For Each , w języku Visual Basic) zwraca obiekt typu elementów w kolekcji. Ponieważ każdy element kolekcji na IDictionary<TKey,TValue> podstawie jest parą klucz/wartość, typ elementu nie jest typem klucza ani typem wartości. Zamiast tego typ elementu to KeyValuePair<TKey,TValue>. Na przykład:

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

Instrukcja foreach jest otoką wokół modułu wyliczającego, który umożliwia odczytywanie tylko z, a nie zapisywania do kolekcji.

Konstruktory

KeyValuePair<TKey,TValue>(TKey, TValue)

Inicjuje KeyValuePair<TKey,TValue> nowe wystąpienie struktury przy użyciu określonego klucza i wartości.

Właściwości

Key

Pobiera klucz w parze klucz/wartość.

Value

Pobiera wartość w parze klucz/wartość.

Metody

Deconstruct(TKey, TValue)

Dekonstruktoruje bieżący KeyValuePair<TKey,TValue>element .

ToString()

Zwraca reprezentację KeyValuePair<TKey,TValue>ciągu elementu , używając reprezentacji ciągu klucza i wartości.

Dotyczy

Zobacz też