IDictionary.Item[Object] Propriété

Définition

Obtient ou définit l'élément à l'aide de la clé spécifiée.

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

Paramètres

key
Object

Clé de l'élément à obtenir ou définir.

Valeur de propriété

L’élément avec la clé spécifiée, ou null si la clé n’existe pas.

Exceptions

key a la valeur null.

La propriété est définie et l'objet IDictionary est en lecture seule.

- ou -

La propriété est définie, key n’existe pas dans la collection et IDictionary a une taille fixe.

Exemples

L’exemple de code suivant montre comment implémenter la Item[] propriété. Cet exemple de code fait partie d’un exemple plus grand fourni pour 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

Remarques

Cette propriété permet d'accéder à un élément spécifique dans la collection à l'aide de la syntaxe suivante : myCollection[key].

Vous pouvez également utiliser la Item[] propriété pour ajouter de nouveaux éléments en définissant la valeur d’une clé qui n’existe pas dans le dictionnaire (par exemple, myCollection["myNonexistentKey"] = myValue). Toutefois, si la clé spécifiée existe déjà dans le dictionnaire, la définition de la Item[] propriété remplace l’ancienne valeur. En revanche, la Add méthode ne modifie pas les éléments existants.

Les implémentations peuvent varier selon qu’elles autorisent la clé à être null.

Le langage C# utilise ce this mot clé pour définir les indexeurs au lieu d’implémenter laItem[] propriété. Visual Basic implémente Item[] comme propriété par défaut, ce qui fournit les mêmes fonctionnalités d'indexation.

S’applique à

Voir aussi