OrderedDictionary.Item[] Proprietà

Definizione

Ottiene o imposta il valore specificato.

Overload

Item[Int32]

Ottiene o imposta il valore in corrispondenza dell'indice specificato.

Item[Object]

Ottiene o imposta il valore con la chiave specificata.

Item[Int32]

Source:
OrderedDictionary.cs
Source:
OrderedDictionary.cs
Source:
OrderedDictionary.cs

Ottiene o imposta il valore in corrispondenza dell'indice specificato.

public:
 property System::Object ^ default[int] { System::Object ^ get(int index); void set(int index, System::Object ^ value); };
public object this[int index] { get; set; }
public object? this[int index] { get; set; }
member this.Item(int) : obj with get, set
Default Public Property Item(index As Integer) As Object

Parametri

index
Int32

Indice in base zero del valore da ottenere o impostare.

Valore della proprietà

Valore dell'elemento in corrispondenza dell'indice specificato.

Implementazioni

Eccezioni

La proprietà viene impostata e l'insieme OrderedDictionary è in sola lettura.

index è minore di zero.

-oppure-

index è uguale a o maggiore di Count.

Commenti

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

Il linguaggio C# usa la parola chiave per definire gli indicizzatori anziché implementare la Item[] proprietà . Visual Basic implementa Item[] come proprietà predefinita, che fornisce la stessa funzionalità di indicizzazione.

Si applica a

Item[Object]

Source:
OrderedDictionary.cs
Source:
OrderedDictionary.cs
Source:
OrderedDictionary.cs

Ottiene o imposta il valore 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 del valore da ottenere o impostare.

Valore della proprietà

Valore associato alla chiave specificata. Se la chiave specificata non viene trovata, tentando di ottenerla viene restituito null mentre tentando di impostarla viene creato un nuovo elemento con la chiave specificata.

Implementazioni

Eccezioni

La proprietà viene impostata e l'insieme OrderedDictionary è in sola lettura.

Esempio

Nell'esempio di codice seguente viene illustrata la modifica di una OrderedDictionary raccolta. In questo esempio la Item[] proprietà viene usata per modificare la voce del dizionario con la chiave "testKey2". Questo codice fa parte di un esempio di codice più ampio che può essere visualizzato in OrderedDictionary.

// Modifying the OrderedDictionary
if (!myOrderedDictionary->IsReadOnly)
{
    // Insert a new key to the beginning of the OrderedDictionary
    myOrderedDictionary->Insert(0, "insertedKey1", "insertedValue1");

    // Modify the value of the entry with the key "testKey2"
    myOrderedDictionary["testKey2"] = "modifiedValue";

    // Remove the last entry from the OrderedDictionary: "testKey3"
    myOrderedDictionary->RemoveAt(myOrderedDictionary->Count - 1);

    // Remove the "keyToDelete" entry, if it exists
    if (myOrderedDictionary->Contains("keyToDelete"))
    {
        myOrderedDictionary->Remove("keyToDelete");
    }
}
// Modifying the OrderedDictionary
if (!myOrderedDictionary.IsReadOnly)
{
    // Insert a new key to the beginning of the OrderedDictionary
    myOrderedDictionary.Insert(0, "insertedKey1", "insertedValue1");

    // Modify the value of the entry with the key "testKey2"
    myOrderedDictionary["testKey2"] = "modifiedValue";

    // Remove the last entry from the OrderedDictionary: "testKey3"
    myOrderedDictionary.RemoveAt(myOrderedDictionary.Count - 1);

    // Remove the "keyToDelete" entry, if it exists
    if (myOrderedDictionary.Contains("keyToDelete"))
    {
        myOrderedDictionary.Remove("keyToDelete");
    }
}
' Modifying the OrderedDictionary
If Not myOrderedDictionary.IsReadOnly Then

    ' Insert a new key to the beginning of the OrderedDictionary
    myOrderedDictionary.Insert(0, "insertedKey1", "insertedValue1")

    ' Modify the value of the entry with the key "testKey2"
    myOrderedDictionary("testKey2") = "modifiedValue"

    ' Remove the last entry from the OrderedDictionary: "testKey3"
    myOrderedDictionary.RemoveAt(myOrderedDictionary.Count - 1)

    ' Remove the "keyToDelete" entry, if it exists
    If (myOrderedDictionary.Contains("keyToDelete")) Then
        myOrderedDictionary.Remove("keyToDelete")
    End If
End If

Commenti

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

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

Una chiave non può essere null, ma un valore può essere . Per distinguere null l'oggetto restituito perché la chiave specificata non viene trovata e null restituita perché il valore della chiave specificata è null, usare il Contains metodo per determinare se la chiave esiste in OrderedDictionary.

Si applica a