IDictionary.Item[Object] 屬性

定義

取得或設定具有指定索引鍵的項目。Gets or sets the element with the specified key.

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; }
member this.Item(obj) : obj with get, set
Default Public Property Item(key As Object) As Object

參數

key
Object

要取得或設定之項目的索引鍵。The key of the element to get or set.

屬性值

具有指定索引鍵的項目;如果索引鍵不存在,則為 nullThe element with the specified key, or null if the key does not exist.

例外狀況

keynullkey 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 code example demonstrates 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].

您也可以藉由設定不存在於字典中之索引鍵的值(例如,myCollection["myNonexistentKey"] = myValue),使用 Item[Object] 屬性來加入新的元素。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.

不論是否允許金鑰 null,都可能會有不同的執行方式。Implementations can vary in whether they allow the key to be null.

C#語言會使用 thisthis關鍵字來定義索引子,而不是執行 Item[Object] 屬性。The C# language uses the thisthis keyword to define the indexers instead of implementing the Item[Object] property. Visual Basic 會將 Item[Object] 實作為預設屬性,這樣會提供相同的索引功能。Visual Basic implements Item[Object] as a default property, which provides the same indexing functionality.

適用於

另請參閱