DictionaryEntry Structure

Defines a dictionary key/value pair that can be set or retrieved.

Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)

public struct DictionaryEntry
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class DictionaryEntry extends ValueType
Not applicable.

The IDictionaryEnumerator.Entry method returns an instance of this type.

The foreach statement of the C# language (for each in Visual C++, For Each in Visual Basic) requires the type of each element in the collection. 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. Instead, the element type is DictionaryEntry. For example:

foreach (DictionaryEntry de in myHashtable) {...}

The foreach statement is a wrapper around the enumerator, which only allows reading from, not writing to, the collection.

The following example demonstrates the use of DictionaryEntry to iterate through a Hashtable object.

// 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 KeyValuePair objects.
        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

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0