OrderedDictionary.Add(Object, Object) Méthode

Définition

Ajoute une entrée avec la clé et la valeur spécifiées dans la collection OrderedDictionary avec l'index disponible le plus bas.

public:
 virtual void Add(System::Object ^ key, System::Object ^ value);
public void Add (object key, object value);
public void Add (object key, object? value);
abstract member Add : obj * obj -> unit
override this.Add : obj * obj -> unit
Public Sub Add (key As Object, value As Object)

Paramètres

key
Object

Clé de l'entrée à ajouter.

value
Object

Valeur de l'entrée à ajouter. Cette valeur peut être null.

Implémente

Exceptions

La collection OrderedDictionary est en lecture seule.

Un élément possédant la même clé existe déjà dans la collection OrderedDictionary.

Exemples

L’exemple de code suivant illustre la création et le remplissage d’une OrderedDictionary collection. Ce code fait partie d’un exemple de code plus large qui peut être consulté à l’adresse OrderedDictionary.

// Creates and initializes a OrderedDictionary.
OrderedDictionary^ myOrderedDictionary = gcnew OrderedDictionary();
myOrderedDictionary->Add("testKey1", "testValue1");
myOrderedDictionary->Add("testKey2", "testValue2");
myOrderedDictionary->Add("keyToDelete", "valueToDelete");
myOrderedDictionary->Add("testKey3", "testValue3");

ICollection^ keyCollection = myOrderedDictionary->Keys;
ICollection^ valueCollection = myOrderedDictionary->Values;

// Display the contents using the key and value collections
DisplayContents(keyCollection, valueCollection, myOrderedDictionary->Count);
// Creates and initializes a OrderedDictionary.
OrderedDictionary myOrderedDictionary = new OrderedDictionary();
myOrderedDictionary.Add("testKey1", "testValue1");
myOrderedDictionary.Add("testKey2", "testValue2");
myOrderedDictionary.Add("keyToDelete", "valueToDelete");
myOrderedDictionary.Add("testKey3", "testValue3");

ICollection keyCollection = myOrderedDictionary.Keys;
ICollection valueCollection = myOrderedDictionary.Values;

// Display the contents using the key and value collections
DisplayContents(keyCollection, valueCollection, myOrderedDictionary.Count);
' Creates and initializes a OrderedDictionary.
Dim myOrderedDictionary As New OrderedDictionary()
myOrderedDictionary.Add("testKey1", "testValue1")
myOrderedDictionary.Add("testKey2", "testValue2")
myOrderedDictionary.Add("keyToDelete", "valueToDelete")
myOrderedDictionary.Add("testKey3", "testValue3")

Dim keyCollection As ICollection = myOrderedDictionary.Keys
Dim valueCollection As ICollection = myOrderedDictionary.Values

' Display the contents Imports the key and value collections
DisplayContents( _
    keyCollection, valueCollection, myOrderedDictionary.Count)

Remarques

Une clé ne peut pas être null, mais une valeur peut l’être.

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 la OrderedDictionary collection . Toutefois, si la clé spécifiée existe déjà dans , OrderedDictionaryla définition de la Item[] propriété remplace l’ancienne valeur. En revanche, la Add méthode ne modifie pas les éléments existants, mais lève ArgumentExceptionà la place .

S’applique à