DictionaryEntry 結構
定義
定義可設定或擷取的字典索引鍵/值組。Defines a dictionary key/value pair that can be set or retrieved.
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 物件。The following example demonstrates the use of DictionaryEntry to iterate through a Hashtable object.
// A simple example for the DictionaryEntry structure.
using namespace System;
using namespace System::Collections;
public ref class Example
{
public:
static void Main()
{
// Create a new hash table.
//
Hashtable^ openWith = gcnew 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();
for each (DictionaryEntry de in openWith)
{
Console::WriteLine("Key = {0}, Value = {1}", de.Key, de.Value);
}
}
};
int main()
{
Example::Main();
}
/* 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.
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 這個類型的實例。The IDictionaryEnumerator.Entry method returns an instance of this type.
重要
我們不建議您將 DictionaryEntry
結構用於新的開發。We don't recommend that you use the DictionaryEntry
structure for new development. 相反地,我們建議您 KeyValuePair<TKey,TValue> 搭配類別使用泛型結構 Dictionary<TKey,TValue> 。Instead, we recommend that you use a generic KeyValuePair<TKey,TValue> structure along with the Dictionary<TKey,TValue> class. 如需詳細資訊,請參閱不應在 GitHub 上 使用非泛型集合 。For more information, see Non-generic collections shouldn't be used on GitHub.
C # foreach 語句和 Visual Basic For Each 語句需要集合中每個元素的型別。The C# foreach statement and the Visual Basic For Each statement require the type of each element in the collection. 由於的每個專案 IDictionary 都是索引鍵/值組,因此元素類型不是索引鍵的類型或值的型別。Since each element of the IDictionary is a key/value pair, the element type is not the type of the key or the type of the value. 相反地,元素類型為 DictionaryEntry 。Instead, the element type is DictionaryEntry. 例如:For example:
for each (DictionaryEntry de in openWith)
{
Console::WriteLine("Key = {0}, Value = {1}", de.Key, de.Value);
}
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
語句是列舉值周圍的包裝函式,它只允許讀取或寫入集合。The foreach
statement is a wrapper around the enumerator, which only allows reading from, not writing to, the collection.
建構函式
DictionaryEntry(Object, Object) |
初始化具有指定索引鍵和值之 DictionaryEntry 型別的執行個體。Initializes an instance of the DictionaryEntry type with the specified key and value. |
屬性
Key |
取得或設定索引鍵/值組配對中的索引鍵。Gets or sets the key in the key/value pair. |
Value |
取得或設定索引鍵/值組配對中的值。Gets or sets the value in the key/value pair. |
方法
Deconstruct(Object, Object) |
解構目前的 DictionaryEntry。Deconstructs the current DictionaryEntry. |