PropertyCollection.IDictionary.Item[Object] Propriedade

Definição

Obtém ou define o elemento com a chave especificada.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; }
member this.System.Collections.IDictionary.Item(obj) : obj with get, set
 Property Item(key As Object) As Object Implements IDictionary.Item

Parâmetros

key
Object

A chave do elemento a ser obtido ou definido.The key of the element to get or set.

Valor da propriedade

O elemento com a chave especificada.The element with the specified key.

Implementações

Exceções

key é null.key is null.

A propriedade é definida e o objeto IDictionary é somente leitura.The property is set and the IDictionary object is read-only.

-ou--or- A propriedade é definida, key não existe na coleção e o IDictionary tem um tamanho fixo.The property is set, key does not exist in the collection, and the IDictionary has a fixed size.

Exemplos

O exemplo a seguir mostra como implementar a propriedade Item[Object].The following example shows how to implement the Item[Object] property. Este exemplo de código faz parte de um exemplo maior fornecido para a classe 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

Comentários

Esta propriedade fornece a capacidade de acessar um elemento específico na coleção usando a seguinte sintaxe: myCollection[key].This property provides the ability to access a specific element in the collection by using the following syntax: myCollection[key].

Você também pode usar a propriedade Item[Object] para adicionar novos elementos definindo o valor de uma chave que não existe no dicionário (por exemplo, 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). No entanto, se a chave especificada já existir no dicionário, a definição da propriedade Item[Object] substituirá o valor antigo.However, if the specified key already exists in the dictionary, setting the Item[Object] property overwrites the old value. Por outro lado, o método Add não modifica os elementos existentes.In contrast, the Add method does not modify existing elements.

Aplica-se a

Veja também