DictionaryEntry 結構

定義

定義可設定或擷取的字典索引鍵/值組。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
繼承
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 都需要集合中每個元素的型別。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 型別的執行個體。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)

適用於

另請參閱