DictionaryEntry 结构
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
定义可以设置或检索的字典键/值对。
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 循环 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 返回此类型的实例。
重要
不建议将 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 语句是枚举器周围的包装器,它只允许从集合(而不是写入到集合)进行读取。
构造函数
| 名称 | 说明 |
|---|---|
| DictionaryEntry(Object, Object) |
使用指定的键和值初始化类型的实例 DictionaryEntry 。 |
属性
| 名称 | 说明 |
|---|---|
| Key |
获取或设置键/值对中的键。 |
| Value |
获取或设置键/值对中的值。 |
方法
| 名称 | 说明 |
|---|---|
| Deconstruct(Object, Object) |
解构当前 DictionaryEntry。 |