DictionaryEntry Struktur

Definisi

Menentukan pasangan kunci/nilai kamus yang dapat diatur atau diambil.

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
Warisan
DictionaryEntry
Atribut

Contoh

Contoh berikut menunjukkan penggunaan DictionaryEntry untuk melakukan iterasi melalui Hashtable objek.

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

Keterangan

Metode mengembalikan IDictionaryEnumerator.Entry instans jenis ini.

Important

Kami tidak menyarankan Anda menggunakan DictionaryEntry struktur untuk pengembangan baru. Sebagai gantinya, kami sarankan Anda menggunakan struktur generik KeyValuePair<TKey,TValue> bersama dengan Dictionary<TKey,TValue> kelas . Untuk informasi selengkapnya, lihat Koleksi non-generik tidak boleh digunakan di GitHub.

Pernyataan C# foreach dan pernyataan Visual Basic Untuk Setiap memerlukan jenis setiap elemen dalam koleksi. Karena setiap elemen IDictionary adalah pasangan kunci/nilai, jenis elemen bukan jenis kunci atau jenis nilai. Sebaliknya, jenis elemennya adalah DictionaryEntry. Contohnya:

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

Pernyataan itu foreach adalah pembungkus di sekitar enumerator, yang hanya memungkinkan membaca dari, tidak menulis ke, koleksi.

Konstruktor

Nama Deskripsi
DictionaryEntry(Object, Object)

Menginisialisasi instans jenis DictionaryEntry dengan kunci dan nilai yang ditentukan.

Properti

Nama Deskripsi
Key

Mendapatkan atau mengatur kunci dalam pasangan kunci/nilai.

Value

Mendapatkan atau mengatur nilai dalam pasangan kunci/nilai.

Metode

Nama Deskripsi
Deconstruct(Object, Object)

Mendekonstruksi saat ini DictionaryEntry.

Berlaku untuk

Lihat juga