KeyValuePair<TKey,TValue> Struct
Definizione
Definisce una coppia chiave/valore che può essere impostata o recuperata.Defines a key/value pair that can be set or retrieved.
generic <typename TKey, typename TValue>
public value class KeyValuePair
[System.Serializable]
public struct KeyValuePair<TKey,TValue>
type KeyValuePair<'Key, 'Value> = struct
Public Structure KeyValuePair(Of TKey, TValue)
Parametri di tipo
- TKey
Tipo di chiave.The type of the key.
- TValue
Tipo di valore.The type of the value.
- Ereditarietà
- Attributi
Esempi
Nell'esempio di codice seguente viene illustrato come enumerare le chiavi e i valori in un dizionario KeyValuePair<TKey,TValue> , usando la struttura.The following code example shows how to enumerate the keys and values in a dictionary, using the KeyValuePair<TKey,TValue> structure.
Questo codice fa parte di un esempio più ampio fornito per Dictionary<TKey,TValue> la classe.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
Commenti
La Dictionary<TKey,TValue>.Enumerator.Current proprietà restituisce un'istanza di questo tipo.The Dictionary<TKey,TValue>.Enumerator.Current property returns an instance of this type.
L' foreach
istruzione del C# linguaggiofor each
(in C++, For Each
in Visual Basic) restituisce un oggetto del tipo degli elementi nella raccolta.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. Poiché ogni elemento di una raccolta basata su IDictionary<TKey,TValue> è una coppia chiave/valore, il tipo di elemento non è il tipo della chiave o il tipo del valore.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. Al contrario, il tipo di KeyValuePair<TKey,TValue>elemento è.Instead, the element type is KeyValuePair<TKey,TValue>. Ad esempio: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
L' foreach
istruzione è un wrapper per l'enumeratore, che consente solo la lettura, non la scrittura, della raccolta.The foreach
statement is a wrapper around the enumerator, which allows only reading from, not writing to, the collection.
Costruttori
KeyValuePair<TKey,TValue>(TKey, TValue) |
Inizializza una nuova istanza della struttura KeyValuePair<TKey,TValue> con la chiave e il valore specificati.Initializes a new instance of the KeyValuePair<TKey,TValue> structure with the specified key and value. |
Proprietà
Key |
Ottiene la chiave della coppia chiave/valore.Gets the key in the key/value pair. |
Value |
Ottiene il valore della coppia chiave/valore.Gets the value in the key/value pair. |
Metodi
Deconstruct(TKey, TValue) | |
ToString() |
Restituisce una rappresentazione di stringa dell'oggetto KeyValuePair<TKey,TValue>, utilizzando le rappresentazioni di stringa della chiave e del valore.Returns a string representation of the KeyValuePair<TKey,TValue>, using the string representations of the key and value. |