OrderedDictionary.Add(Object, Object) Metodo

Definizione

Aggiunge alla raccolta OrderedDictionary una voce contenente la chiave e il valore specificati in corrispondenza dell'indice più basso disponibile.

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)

Parametri

key
Object

Chiave della voce da aggiungere.

value
Object

Valore della voce da aggiungere. Il valore può essere null.

Implementazioni

Eccezioni

L'insieme OrderedDictionary è in sola lettura.

Un elemento con la stessa chiave esiste già nell'insieme OrderedDictionary.

Esempio

L'esempio di codice seguente illustra la creazione e la popolazione di una OrderedDictionary raccolta. Questo codice fa parte di un esempio di codice più grande che può essere visualizzato in 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)

Commenti

Una chiave non può essere null, ma un valore può essere.

È anche possibile usare la Item[] proprietà per aggiungere nuovi elementi impostando il valore di una chiave che non esiste nell'insieme OrderedDictionary . Tuttavia, se la chiave specificata esiste già in OrderedDictionary, impostando la proprietà sovrascrive il Item[] valore precedente. Al contrario, il Add metodo non modifica gli elementi esistenti, ma genera ArgumentExceptioninvece .

Si applica a