OrderedDictionary.Add(Object, Object) Metoda
Definicja
Dodaje wpis z określonym kluczem i wartością do OrderedDictionary kolekcji z najniższym dostępnym indeksem.Adds an entry with the specified key and value into the OrderedDictionary collection with the lowest available index.
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.The key of the entry to add.
- value
- Object
Wartość wpisu do dodania.The value of the entry to add. Ta wartość może być równa null
.This value can be null
.
Implementuje
Wyjątki
OrderedDictionaryKolekcja jest tylko do odczytu.The OrderedDictionary collection is read-only.
Element z tym samym kluczem już istnieje w OrderedDictionary kolekcji.An element with the same key already exists in the OrderedDictionary collection.
Przykłady
Poniższy przykład kodu demonstruje tworzenie i wypełnianie OrderedDictionary kolekcji.The following code example demonstrates the creation and population of an OrderedDictionary collection. Ten kod jest częścią większego przykładu kodu, który może być wyświetlany w OrderedDictionary .This code is part of a larger code example that can be viewed at 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
, ale może być wartością.A key cannot be null
, but a value can be.
Można również użyć właściwości, Item[] Aby dodać nowe elementy przez ustawienie wartości klucza, który nie istnieje w OrderedDictionary kolekcji, jednak jeśli określony klucz już istnieje w OrderedDictionary , ustawienie Item[] Właściwości zastępuje starą wartość.You can also use the Item[] property to add new elements by setting the value of a key that does not exist in the OrderedDictionary collection; however, if the specified key already exists in the OrderedDictionary, setting the Item[] property overwrites the old value. Z kolei Add Metoda nie modyfikuje istniejących elementów, ale zamiast tego zgłasza ArgumentException .In contrast, the Add method does not modify existing elements but instead throws ArgumentException.