DictionaryEntry 結構

定義

定義可設定或擷取的字典索引鍵/值組。

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
繼承
DictionaryEntry
屬性

範例

以下範例展示了如何使用 來 DictionaryEntry 遍歷物件 Hashtable

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

備註

IDictionaryEnumerator.Entry 方法回傳此類型的實例。

Important

我們不建議你用這個 DictionaryEntry 結構來進行新開發。 相反地,我們建議你在類別中使用通用 KeyValuePair<TKey,TValue> 結構 Dictionary<TKey,TValue> 。 如需詳細資訊,請參閱 gitHub 上不應 使用非泛型集合

C# foreach 陳述式與 Visual Basic For Each 陳述式需要集合中每個元素的型別。 由於 的 IDictionary 每個元素都是鍵值對,因此元素類型既不是鍵的類型,也不是值的類型。 取而代之的是元素類型。DictionaryEntry 例如:

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

這個 foreach 陳述句是包裹在列舉器上的封裝器,只允許從集合讀取,不能寫入。

建構函式

名稱 Description
DictionaryEntry(Object, Object)

初始化該類型的實例 DictionaryEntry ,並以指定的鍵值與值。

屬性

名稱 Description
Key

取得或設定鍵值對中的鍵。

Value

取得或設定鍵值對中的值。

方法

名稱 Description
Deconstruct(Object, Object)

解構了當前的 DictionaryEntry

適用於

另請參閱