IDictionary.Item[Object] プロパティ

定義

指定したキーを持つ要素を取得または設定します。

public:
 property System::Object ^ default[System::Object ^] { System::Object ^ get(System::Object ^ key); void set(System::Object ^ key, System::Object ^ value); };
public object this[object key] { get; set; }
public object? this[object key] { get; set; }
member this.Item(obj) : obj with get, set
Default Public Property Item(key As Object) As Object

パラメーター

key
Object

取得または設定する要素のキー。

プロパティ値

指定したキーを持つ要素。該当するキーが存在しない場合は null

例外

keynullです。

このプロパティが設定されていますが、IDictionary オブジェクトが読み取り専用です。

- または -

プロパティが設定済みで、コレクション内に key が存在せず、IDictionary が固定サイズです。

次のコード例では、 プロパティを実装する方法を Item[] 示します。 このコード例は、IDictionary クラスのために提供されている大規模な例の一部です。

public:
    virtual property Object^ default[Object^]
    {
        Object^ get(Object^ key)
        {
            // If this key is in the dictionary, return its value.
            int index;
            if (TryGetIndexOfKey(key, &index))
            {
                // The key was found; return its value.
                return items[index]->Value;
            }
            else
            {
                // The key was not found; return null.
                return nullptr;
            }
        }

        void set(Object^ key, Object^ value)
        {
            // If this key is in the dictionary, change its value.
            int index;
            if (TryGetIndexOfKey(key, &index))
            {
                // The key was found; change its value.
                items[index]->Value = value;
            }
            else
            {
                // This key is not in the dictionary; add this
                // key/value pair.
                Add(key, value);
            }
        }
    }
public object this[object key]
{
    get
    {
        // If this key is in the dictionary, return its value.
        Int32 index;
        if (TryGetIndexOfKey(key, out index))
        {
            // The key was found; return its value.
            return items[index].Value;
        }
        else
        {
            // The key was not found; return null.
            return null;
        }
    }

    set
    {
        // If this key is in the dictionary, change its value.
        Int32 index;
        if (TryGetIndexOfKey(key, out index))
        {
            // The key was found; change its value.
            items[index].Value = value;
        }
        else
        {
            // This key is not in the dictionary; add this key/value pair.
            Add(key, value);
        }
    }
}
Public Property Item(ByVal key As Object) As Object Implements IDictionary.Item
    Get

        ' If this key is in the dictionary, return its value.
        Dim index As Integer
        If TryGetIndexOfKey(key, index) Then

            ' The key was found return its value.
            Return items(index).Value
        Else

            ' The key was not found return null.
            Return Nothing
        End If
    End Get

    Set(ByVal value As Object)
        ' If this key is in the dictionary, change its value. 
        Dim index As Integer
        If TryGetIndexOfKey(key, index) Then

            ' The key was found change its value.
            items(index).Value = value
        Else

            ' This key is not in the dictionary add this key/value pair.
            Add(key, value)
        End If
    End Set
End Property

注釈

このプロパティは、構文 myCollection[key] を使用して、コレクションの特定の要素にアクセスする機能を提供します。

プロパティを Item[] 使用して、ディクショナリに存在しないキーの値 (例: ) を設定して、 myCollection["myNonexistentKey"] = myValue新しい要素を追加することもできます。 ただし、指定したキーがディクショナリに既に存在する場合は、 プロパティを Item[] 設定すると古い値が上書きされます。 これに対し、 メソッドは既存の Add 要素を変更しません。

実装は、キーを に許可するかどうかによって異なる場合があります null

C# 言語では、 プロパティをthis実装する代わりに、このキーワードを使用してインデクサーをItem[]定義します。 Visual Basic は、Item[] を既定のプロパティとして実装しており、同様のインデックス機能を提供します。

適用対象

こちらもご覧ください