PropertyCollection.IDictionary.Item[Object] Proprietà

Definizione

Ottiene o imposta l'elemento con la chiave specificata.

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

Parametri

key
Object

Chiave dell'elemento da ottenere o impostare.

Valore della proprietà

Elemento con la chiave specificata.

Implementazioni

Eccezioni

key è null.

La proprietà è impostata e l'oggetto IDictionary è di sola lettura.

oppure

La proprietà è impostata, key non esiste nell'insieme e ha IDictionary una dimensione fissa.

Esempio

Nell'esempio seguente viene illustrato come implementare la Item[] proprietà . Questo esempio di codice fa parte di un esempio più ampio fornito per la 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

Commenti

Questa proprietà consente di accedere a un elemento specifico dell'insieme usando la sintassi seguente: myCollection[key].

È anche possibile usare la Item[] proprietà per aggiungere nuovi elementi impostando il valore di una chiave che non esiste nel dizionario , ad esempio myCollection["myNonexistentKey"] = myValue. Tuttavia, se la chiave specificata esiste già nel dizionario, l'impostazione della Item[] proprietà sovrascrive il valore precedente. Al contrario, il Add metodo non modifica gli elementi esistenti.

Si applica a

Vedi anche