IDictionary.Item[Object] Propriedade

Definição

Obtém ou define o elemento com a chave especificada.

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

Parâmetros

key
Object

A chave do elemento a ser obtida ou adicionada.

Valor da propriedade

O elemento com a chave especificada ou null, se a chave não existir.

Exceções

key é null.

A propriedade é definida e o objeto IDictionary é somente leitura.

- ou -

A propriedade é definida, key não existe na coleção e o IDictionary tem um tamanho fixo.

Exemplos

O exemplo de código a seguir demonstra como implementar a Item[] propriedade . Este exemplo de código faz parte de um exemplo maior fornecido para a IDictionary classe .

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

Você também pode usar a Item[] propriedade para adicionar novos elementos definindo o valor de uma chave que não existe no dicionário (por exemplo, myCollection["myNonexistentKey"] = myValue). No entanto, se a chave especificada já existir no dicionário, definir a Item[] propriedade substituirá o valor antigo. Por outro lado, o Add método não modifica os elementos existentes.

As implementações podem variar se permitem que a chave seja null.

A linguagem C# usa essa this palavra-chave para definir os indexadores em vez de implementar aItem[] propriedade . O Visual Basic implementa Item[] como uma propriedade padrão, que fornece a mesma funcionalidade de indexação.

Aplica-se a

Confira também