OrderedDictionary.Add(Object, Object) Metoda
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Dodaje wpis z określonym kluczem i wartością OrderedDictionary do kolekcji z najniższym dostępnym indeksem.
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)
Parametry
- key
- Object
Klucz wpisu do dodania.
- value
- Object
Wartość wpisu do dodania. Ta wartość może mieć wartość null
.
Implementuje
Wyjątki
Kolekcja OrderedDictionary jest tylko do odczytu.
Element o tym samym kluczu już istnieje w kolekcji OrderedDictionary .
Przykłady
Poniższy przykład kodu przedstawia tworzenie i populację OrderedDictionary kolekcji. Ten kod jest częścią większego przykładu kodu, który można wyświetlić pod adresem 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)
Uwagi
Klucz nie może być null
wartością , ale może to być wartość.
Możesz również użyć Item[] właściwości , aby dodać nowe elementy, ustawiając wartość klucza, który nie istnieje w OrderedDictionary kolekcji, jednak jeśli określony klucz już istnieje w OrderedDictionaryobiekcie , ustawienie Item[] właściwości zastępuje starą wartość. Natomiast metoda nie modyfikuje ArgumentExceptionistniejących elementów, Add ale zamiast tego zgłasza wyjątek .