PropertyCollection.IDictionary.Item[Object] プロパティ

定義

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

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

パラメーター

key
Object

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

プロパティ値

Object

指定したキーを持つ要素。

実装

例外

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 変更しません。

適用対象

こちらもご覧ください