DictionaryEntry DictionaryEntry DictionaryEntry DictionaryEntry Struct

定義

定義可設定或擷取的字典索引鍵/值組配對。Defines a dictionary key/value pair that can be set or retrieved.

public value class DictionaryEntry
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public struct DictionaryEntry
type DictionaryEntry = struct
Public Structure DictionaryEntry
繼承
DictionaryEntryDictionaryEntryDictionaryEntryDictionaryEntry
屬性

範例

下列範例示範如何使用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
Imports System.Collections
Imports Microsoft.VisualBasic

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每個陳述式需要集合中每個元素的類型。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. 相反地,項目型別是DictionaryEntryInstead, 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(Object, Object) DictionaryEntry(Object, Object) DictionaryEntry(Object, Object)

初始化具有指定索引鍵和值之 DictionaryEntry 型別的執行個體。Initializes an instance of the DictionaryEntry type with the specified key and value.

屬性

Key Key Key Key

取得或設定索引鍵/值組配對中的索引鍵。Gets or sets the key in the key/value pair.

Value Value Value Value

取得或設定索引鍵/值組配對中的值。Gets or sets the value in the key/value pair.

方法

Deconstruct(Object, Object) Deconstruct(Object, Object) Deconstruct(Object, Object) Deconstruct(Object, Object)

適用於

另請參閱