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

要取得或設定之項目的索引鍵。

屬性值

具有指定索引鍵的項目。

實作

例外狀況

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 方法不會修改現有的專案。

適用於

另請參閱