IDictionary.Item[Object] Proprietà
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Ottiene o imposta l'elemento con la chiave specificata.
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
Parametri
- key
- Object
Chiave dell'elemento da ottenere o impostare.
Valore della proprietà
Elemento con la chiave specificata oppure null se la chiave non esiste.
Eccezioni
key è null.
La proprietà è impostata e l'oggetto IDictionary è in sola lettura.
-oppure-
La proprietà è impostata, key non esiste nella raccolta e IDictionary è di dimensioni fisse.
Esempio
Nell'esempio di codice 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:
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
Commenti
Questa proprietà consente di accedere a un elemento specifico nella raccolta utilizzando la sintassi seguente: myCollection[key].
È anche possibile utilizzare 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.
Le implementazioni possono variare in base al fatto che consentano la chiave come null.
Il linguaggio C# usa la this parola chiave per definire gli indicizzatori anziché implementare la Item[] proprietà . In Visual Basic la proprietà Item[] viene implementata come predefinita per fornire la stessa funzionalità di indicizzazione.