KeyValuePair<TKey,TValue> Struct
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Definisce una coppia chiave/valore che può essere impostata o recuperata.
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
Tipo di chiave.
- TValue
Tipo di valore.
- Ereditarietà
- Attributi
Nell'esempio di codice seguente viene illustrato come enumerare le chiavi e i valori in un dizionario usando la KeyValuePair<TKey,TValue> struttura.
Questo codice fa parte di un esempio più grande fornito per la Dictionary<TKey,TValue> classe.
// 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
La proprietà restituisce un'istanza Dictionary<TKey,TValue>.Enumerator.Current di questo tipo.
L'istruzione foreach
del linguaggio C# (for each
in C++, For Each
in Visual Basic) restituisce un oggetto del tipo degli elementi nell'insieme. 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. Il tipo di elemento è KeyValuePair<TKey,TValue>invece . Ad esempio:
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'istruzione foreach
è un wrapper intorno all'enumeratore, che consente solo la lettura da, non la scrittura in, la raccolta.
Key |
Inizializza una nuova istanza della struttura KeyValuePair<TKey,TValue> con la chiave e il valore specificati. |
Key |
Ottiene la chiave della coppia chiave/valore. |
Value |
Ottiene il valore della coppia chiave/valore. |
Deconstruct(TKey, TValue) |
Decostruisce la struttura KeyValuePair<TKey,TValue> corrente. |
To |
Restituisce una rappresentazione di stringa dell'oggetto KeyValuePair<TKey,TValue>, usando le rappresentazioni di stringa della chiave e del valore. |
Prodotto | Versioni |
---|---|
.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 |
Feedback su .NET
.NET è un progetto di open source. Selezionare un collegamento per fornire feedback: