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; }
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 para obter ou definir.

Valor de Propriedade

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

Exceções

key é null.

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

-ou-

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

Exemplos

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

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

Observações

Esta propriedade permite aceder a um elemento específico da coleção utilizando a seguinte sintaxe: myCollection[key].

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 sobrescreve o valor antigo. Em contraste, o Add método não modifica elementos existentes.

As implementações podem variar quanto a permitir que a chave seja null.

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

Aplica-se a

Ver também