DictionaryEntry 構造体

定義

設定または取得できるディクショナリ キーと値のペアを定義します。

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
属性

次の例では、 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 メソッドは、この型のインスタンスを返します。

Important

新しい開発には DictionaryEntry 構造を使用しないことをお勧めします。 代わりに、Dictionary<TKey,TValue> クラスと共にジェネリック KeyValuePair<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を分解します。

適用対象

こちらもご覧ください