DictionaryEntry Struct-datatyp

Definition

Definierar ett nyckel/värde-par i ordlistan som kan anges eller hämtas.

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
Arv
DictionaryEntry
Attribut

Exempel

I följande exempel visas hur du använder DictionaryEntry för att iterera genom ett Hashtable objekt.

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

Kommentarer

Metoden IDictionaryEnumerator.Entry returnerar en instans av den här typen.

Important

Vi rekommenderar inte att du använder DictionaryEntry strukturen för ny utveckling. I stället rekommenderar vi att du använder en allmän KeyValuePair<TKey,TValue> struktur tillsammans med Dictionary<TKey,TValue> klassen. Mer information finns i Non-generic samlingar bör inte användas på GitHub.

Instruktionen C# foreach och instruktionen Visual Basic For Each kräver typen av varje element i samlingen. Eftersom varje element i IDictionary är ett nyckel/värde-par är elementtypen inte typen av nyckel eller typen av värde. Elementtypen är DictionaryEntryi stället . Ett exempel:

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

-instruktionen foreach är en omslutning runt uppräknaren, som endast tillåter läsning från, inte skriva till, samlingen.

Konstruktorer

Name Description
DictionaryEntry(Object, Object)

Initierar en instans av DictionaryEntry typen med den angivna nyckeln och värdet.

Egenskaper

Name Description
Key

Hämtar eller anger nyckeln i nyckel/värde-paret.

Value

Hämtar eller anger värdet i nyckel/värde-paret.

Metoder

Name Description
Deconstruct(Object, Object)

Dekonstruerar den aktuella DictionaryEntry.

Gäller för

Se även