KeyValuePair<TKey,TValue> Estructura

Definición

Define un par clave-valor que se puede establecer o recuperar.

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)

Parámetros de tipo

TKey

Tipo de la clave.

TValue

Tipo del valor.

Herencia
KeyValuePair<TKey,TValue>
Atributos

Ejemplos

En el ejemplo de código siguiente se muestra cómo enumerar las claves y los valores de un diccionario mediante la KeyValuePair<TKey,TValue> estructura .

Este código forma parte de un ejemplo más grande proporcionado para la Dictionary<TKey,TValue> clase .

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

Comentarios

La Dictionary<TKey,TValue>.Enumerator.Current propiedad devuelve una instancia de este tipo.

La foreach instrucción del lenguaje C# (for each en C++, For Each en Visual Basic) devuelve un objeto del tipo de los elementos de la colección. Puesto que cada elemento de una colección basado en IDictionary<TKey,TValue> es un par clave-valor, el tipo de elemento no es el tipo de la clave o el tipo del valor. En su lugar, el tipo de elemento es KeyValuePair<TKey,TValue>. Por ejemplo:

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

La foreach instrucción es un contenedor alrededor del enumerador, que solo permite leer, no escribir en la colección.

Constructores

KeyValuePair<TKey,TValue>(TKey, TValue)

Inicializa una nueva instancia de la estructura KeyValuePair<TKey,TValue> con la clave y valor especificados.

Propiedades

Key

Obtiene la clave del par clave-valor.

Value

Obtiene el valor del par clave-valor.

Métodos

Deconstruct(TKey, TValue)

Deconstruye el elemento KeyValuePair<TKey,TValue> actual.

ToString()

Devuelve una representación de cadena de KeyValuePair<TKey,TValue>, utilizando las representaciones de cadena de la clave y el valor.

Se aplica a

Consulte también