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

另请参阅