DictionaryEntry 結構

定義

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

C#
public struct DictionaryEntry
C#
[System.Serializable]
public struct DictionaryEntry
C#
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public struct DictionaryEntry
繼承
DictionaryEntry
屬性

範例

下列範例示範 如何使用 DictionaryEntry 來逐一 Hashtable 查看 物件。

C#
// 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
 */

備註

方法會 IDictionaryEnumerator.Entry 傳回這個類型的實例。

重要

不建議您將 結構用於 DictionaryEntry 新的開發。 相反地,建議您搭配 類別使用泛型 KeyValuePair<TKey,TValue> 結構 Dictionary<TKey,TValue> 。 如需詳細資訊,請參閱 不應在 GitHub 上使用非泛型集合

C# foreach 語句和 Visual Basic For Each 語句需要集合中每個元素的類型。 因為的每個 IDictionary 元素都是索引鍵/值組,所以項目類型不是索引鍵的類型或值的型別。 相反地,元素類型為 DictionaryEntry。 例如:

C#
foreach (DictionaryEntry de in openWith)
{
    Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value);
}

語句 foreach 是列舉值周圍的包裝函式,它只允許讀取集合,而不寫入集合。

建構函式

DictionaryEntry(Object, Object)

初始化具有指定索引鍵和值之 DictionaryEntry 型別的執行個體。

屬性

Key

取得或設定索引鍵/值組配對中的索引鍵。

Value

取得或設定索引鍵/值組配對中的值。

方法

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

另請參閱