DictionaryEntry Structure

Définition

Définit une paire clé/valeur du dictionnaire qui peut être définie ou récupérée.Defines a dictionary key/value pair that can be set or retrieved.

public value class DictionaryEntry
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public struct DictionaryEntry
type DictionaryEntry = struct
Public Structure DictionaryEntry
Héritage
DictionaryEntry
Attributs

Exemples

L’exemple suivant illustre l’utilisation de DictionaryEntry pour itérer au sein Hashtable d’un objet.The following example demonstrates the use of DictionaryEntry to iterate through a Hashtable object.

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

public ref class Example
{
public:
    static void Main()
    {
        // Create a new hash table.
        //
        Hashtable^ openWith = gcnew 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();
        for each (DictionaryEntry de in openWith)
        {
            Console::WriteLine("Key = {0}, Value = {1}", de.Key, de.Value);
        }
    }
};

int main()
{
    Example::Main();
}

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

Remarques

La IDictionaryEnumerator.Entry méthode retourne une instance de ce type.The IDictionaryEnumerator.Entry method returns an instance of this type.

Important

Nous vous déconseillons d’utiliser DictionaryEntry la structure pour un nouveau développement.We don't recommend that you use the DictionaryEntry structure for new development. Au lieu de cela, nous vous recommandons d' KeyValuePair<TKey,TValue> utiliser une structure générique Dictionary<TKey,TValue> avec la classe.Instead, we recommend that you use a generic KeyValuePair<TKey,TValue> structure along with the Dictionary<TKey,TValue> class. Pour plus d’informations, consultez collections non génériques ne doit pas être utilisé sur GitHub.For more information, see Non-generic collections shouldn't be used on GitHub.

L' C# instruction foreach et la Visual Basic pour chaque instruction requièrent le type de chaque élément de la collection.The C# foreach statement and the Visual Basic For Each statement require the type of each element in the collection. Étant donné que chaque élément IDictionary de est une paire clé/valeur, le type d’élément n’est pas le type de la clé ou le type de la valeur.Since each element of the IDictionary is a key/value pair, the element type is not the type of the key or the type of the value. Au lieu de cela, le DictionaryEntrytype d’élément est.Instead, the element type is DictionaryEntry. Par exemple :For example:

for each (DictionaryEntry de in openWith)
{
    Console::WriteLine("Key = {0}, Value = {1}", de.Key, de.Value);
}
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

L' foreach instruction est un wrapper autour de l’énumérateur, qui autorise uniquement la lecture, et non l’écriture, de la collection.The foreach statement is a wrapper around the enumerator, which only allows reading from, not writing to, the collection.

Constructeurs

DictionaryEntry(Object, Object)

Initialise une instance du type DictionaryEntry à l'aide de la clé et de la valeur spécifiées.Initializes an instance of the DictionaryEntry type with the specified key and value.

Propriétés

Key

Obtient ou définit la clé dans la paire clé/valeur.Gets or sets the key in the key/value pair.

Value

Obtient ou définit la valeur dans la paire clé/valeur.Gets or sets the value in the key/value pair.

Méthodes

Deconstruct(Object, Object)

S’applique à

Voir aussi