KeyValuePair<TKey,TValue> Struktura
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
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)
- TKey
Typ klucza.
- TValue
Typ wartości.
- Dziedziczenie
- Atrybuty
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
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.
Key |
Inicjuje KeyValuePair<TKey,TValue> nowe wystąpienie struktury przy użyciu określonego klucza i wartości. |
Deconstruct(TKey, TValue) |
Dekonstruktoruje bieżący KeyValuePair<TKey,TValue>element . |
To |
Zwraca reprezentację KeyValuePair<TKey,TValue>ciągu elementu , używając reprezentacji ciągu klucza i wartości. |
Produkt | Wersje |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
Opinia o produkcie .NET
.NET to projekt typu open source. Wybierz link, aby przekazać opinię: