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].

你还可以使用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.

实现在是否允许键null时可能有所不同。Implementations can vary in whether they allow the key to be null.

该C#语言使用this 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.

适用于

另请参阅