DictionaryEntry Estrutura

Definição

Define um par chave/valor de dicionário que pode ser definido ou recuperado.

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
Herança
DictionaryEntry
Atributos

Exemplos

O exemplo a seguir demonstra o uso de iterar por meio de DictionaryEntry um 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

Comentários

O IDictionaryEnumerator.Entry método retorna uma instância desse tipo.

Importante

Não recomendamos que você use a DictionaryEntry estrutura para o novo desenvolvimento. Em vez disso, recomendamos que você use uma estrutura genérica KeyValuePair<TKey,TValue> junto com a Dictionary<TKey,TValue> classe. Para obter mais informações, consulte coleções não genéricas que não devem ser usadas no GitHub.

A instrução C# foreach e a instrução Visual Basic For Each exigem o tipo de cada elemento na coleção. Como cada elemento do IDictionary é um par chave/valor, o tipo de elemento não é o tipo da chave ou o tipo do valor. Em vez disso, o tipo de elemento é DictionaryEntry. Por exemplo:

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

A foreach instrução é um wrapper em torno do enumerador, que só permite a leitura de, não gravando para, a coleção.

Construtores

Nome Description
DictionaryEntry(Object, Object)

Inicializa uma instância do DictionaryEntry tipo com a chave e o valor especificados.

Propriedades

Nome Description
Key

Obtém ou define a chave no par chave/valor.

Value

Obtém ou define o valor no par chave/valor.

Métodos

Nome Description
Deconstruct(Object, Object)

Desconstrui o atual DictionaryEntry.

Aplica-se a

Confira também