IDictionary.Item[Object] IDictionary.Item[Object] IDictionary.Item[Object] IDictionary.Item[Object] Property

定義

取得或設定具有指定索引鍵的項目。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 Object Object Object

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

屬性值

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

例外狀況

已設定該屬性,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].

您也可以使用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.

實作會有所不同是否允許索引鍵為nullImplementations can vary in whether they allow the key to be null.

使用 C# 語言this關鍵字來定義索引,而不必實作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.

適用於

另請參閱