KeyValuePair<TKey,TValue> Struktura
Definicja
Definiuje parę klucz/wartość, która może być ustawiona lub pobrana.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 klucza.The type of the key.
- TValue
Typ wartości.The type of the value.
- Dziedziczenie
- Atrybuty
Przykłady
Poniższy przykład kodu pokazuje, jak wyliczyć klucze i wartości w słowniku przy użyciu 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.
Ten kod jest częścią większego przykładu dostarczonego dla Dictionary<TKey,TValue> klasy.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
Uwagi
Dictionary<TKey,TValue>.Enumerator.CurrentWłaściwość zwraca wystąpienie tego typu.The Dictionary<TKey,TValue>.Enumerator.Current property returns an instance of this type.
foreach
Instrukcja języka C# ( for each
w języku C++, For Each
w Visual Basic) zwraca obiekt typu elementów w kolekcji.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. Ponieważ każdy element kolekcji na podstawie IDictionary<TKey,TValue> jest parą klucz/wartość, typ elementu nie jest typem klucza lub typem wartości.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. Zamiast tego typ elementu to KeyValuePair<TKey,TValue> .Instead, the element type is KeyValuePair<TKey,TValue>. Na przykład: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
Instrukcja to otoka wokół modułu wyliczającego, który umożliwia odczytywanie tylko z kolekcji.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) |
Inicjuje nowe wystąpienie KeyValuePair<TKey,TValue> struktury z określonym kluczem i wartością.Initializes a new instance of the KeyValuePair<TKey,TValue> structure with the specified key and value. |
Właściwości
Key |
Pobiera klucz z pary klucz/wartość.Gets the key in the key/value pair. |
Value |
Pobiera wartość z pary klucz/wartość.Gets the value in the key/value pair. |
Metody
Deconstruct(TKey, TValue) |
Dekonstrukcjauje bieżące KeyValuePair<TKey,TValue> .Deconstructs the current KeyValuePair<TKey,TValue>. |
ToString() |
Zwraca reprezentację ciągu z KeyValuePair<TKey,TValue> , przy użyciu reprezentacji ciągu klucza i wartości.Returns a string representation of the KeyValuePair<TKey,TValue>, using the string representations of the key and value. |