Ler em inglês

Compartilhar via


DictionaryEntry Estrutura

Definição

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

C#
public struct DictionaryEntry
C#
[System.Serializable]
public struct DictionaryEntry
C#
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public struct DictionaryEntry
Herança
DictionaryEntry
Atributos

Exemplos

O exemplo a seguir demonstra o uso de DictionaryEntry para iterar por meio de um Hashtable objeto .

C#
// 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
 */

Comentários

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

Importante

Não recomendamos que você use a DictionaryEntry estrutura para 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 não devem ser usadas no GitHub.

A instrução foreach do C# 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:

C#
foreach (DictionaryEntry de in openWith)
{
    Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value);
}

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

Construtores

DictionaryEntry(Object, Object)

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

Propriedades

Key

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

Value

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

Métodos

Aplica-se a

Produto Versões
.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 1.1, 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

Confira também