DictionaryEntry Estructura

Definición

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

public value class DictionaryEntry
public struct DictionaryEntry
[System.Serializable]
public struct DictionaryEntry
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public struct DictionaryEntry
type DictionaryEntry = struct
[<System.Serializable>]
type DictionaryEntry = struct
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type DictionaryEntry = struct
Public Structure DictionaryEntry
Herencia
DictionaryEntry
Atributos

Ejemplos

En el ejemplo siguiente se muestra el uso de DictionaryEntry para recorrer en iteración un Hashtable objeto .

// A simple example for the DictionaryEntry structure.
using System;
using System.Collections;

class Example
{
    public static void Main()
    {
        // Create a new hash table.
        //
        Hashtable openWith = new Hashtable();

        // Add some elements to the hash table. There are no
        // duplicate keys, but some of the values are duplicates.
        openWith.Add("txt", "notepad.exe");
        openWith.Add("bmp", "paint.exe");
        openWith.Add("dib", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");

        // When you use foreach to enumerate hash table elements,
        // the elements are retrieved as DictionaryEntry objects.
        Console.WriteLine();
        foreach (DictionaryEntry de in openWith)
        {
            Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value);
        }
    }
}

/* This code example produces output similar to the following:

Key = rtf, Value = wordpad.exe
Key = txt, Value = notepad.exe
Key = dib, Value = paint.exe
Key = bmp, Value = paint.exe
 */
'A simple example for the DictionaryEntry structure.
Imports System.Collections

Module Example

    Sub Main()

        ' Create a new hash table.
        '
        Dim openWith As New Hashtable()

        ' Add some elements to the hash table. There are no
        ' duplicate keys, but some of the values are duplicates.
        openWith.Add("txt", "notepad.exe")
        openWith.Add("bmp", "paint.exe")
        openWith.Add("dib", "paint.exe")
        openWith.Add("rtf", "wordpad.exe")

        ' When you use For Each to enumerate hash table elements,
        ' the elements are retrieved as DictionaryEntry objects.
        Console.WriteLine()
        For Each de As DictionaryEntry In openWith
            Console.WriteLine("Key = {0}, Value = {1}", _
                de.Key, de.Value)
        Next de

    End Sub

End Module

' This code example produces output similar to the following:
'
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe
'Key = dib, Value = paint.exe
'Key = bmp, Value = paint.exe

Comentarios

El IDictionaryEnumerator.Entry método devuelve una instancia de este tipo.

Importante

No se recomienda usar la DictionaryEntry estructura para el nuevo desarrollo. En su lugar, se recomienda usar una estructura genérica KeyValuePair<TKey,TValue> junto con la Dictionary<TKey,TValue> clase . Para obtener más información, consulte Colecciones no genéricas no deben usarse en GitHub.

La instrucción foreach y la instrucción Visual Basic For Each requieren el tipo de cada elemento de la colección. Dado que cada elemento de IDictionary 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 DictionaryEntry. Por ejemplo:

foreach (DictionaryEntry de in openWith)
{
    Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value);
}
For Each de As DictionaryEntry In openWith
    Console.WriteLine("Key = {0}, Value = {1}", _
        de.Key, de.Value)
Next de

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

Constructores

Nombre Description
DictionaryEntry(Object, Object)

Inicializa una instancia del DictionaryEntry tipo con la clave y el valor especificados.

Propiedades

Nombre Description
Key

Obtiene o establece la clave en el par clave-valor.

Value

Obtiene o establece el valor en el par clave-valor.

Métodos

Nombre Description
Deconstruct(Object, Object)

Deconstruye el objeto actual DictionaryEntry.

Se aplica a

Consulte también