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

定義

指定したキーを持つ要素を取得または設定します。Gets or sets the element with the specified key.

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; }
 Property Item(key As Object) As Object Implements IDictionary.Item

パラメーター

key
Object

取得または設定する要素のキー。The key of the element to get or set.

プロパティ値

指定したキーを持つ要素。The element with the specified key.

実装

例外

keynullです。key is null.

このプロパティが設定されていますが、IDictionary オブジェクトが読み取り専用です。The property is set and the IDictionary object is read-only.

- または --or- プロパティが設定済みで、コレクション内に key が存在せず、IDictionary が固定サイズです。The property is set, key does not exist in the collection, and the IDictionary has a fixed size.

次の例は、 Item[Object]プロパティを実装する方法を示しています。The following example shows how to implement the Item[Object] property. このコード例は、 IDictionaryクラスに対して提供される大規模な例の一部です。This code example is part of a larger example provided for the IDictionary class.

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] という構文を使用して、コレクション内の特定の要素にアクセスできます。This property provides the ability to access a specific element in the collection by using the following syntax: myCollection[key].

また、 Item[Object]プロパティを使用して、ディクショナリに存在しないキーの値 (などmyCollection["myNonexistentKey"] = myValue) を設定することによって、新しい要素を追加することもできます。You can also use the Item[Object] property to add new elements by setting the value of a key that does not exist in the dictionary (for example, myCollection["myNonexistentKey"] = myValue). ただし、指定したキーがディクショナリに既に存在する場合Item[Object]は、プロパティを設定すると古い値が上書きされます。However, if the specified key already exists in the dictionary, setting the Item[Object] property overwrites the old value. これに対してAdd 、メソッドは既存の要素を変更しません。In contrast, the Add method does not modify existing elements.

適用対象

こちらもご覧ください