Share via


OrderedDictionary.Add(Object, Object) Método

Definição

Adiciona uma entrada com a chave e o valor especificados à coleção OrderedDictionary com o índice mais baixo disponível.

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)

Parâmetros

key
Object

A chave da entrada a ser adicionada.

value
Object

O valor da entrada a ser adicionado. Este valor pode ser null.

Implementações

Exceções

A coleção OrderedDictionary é somente leitura.

Já existe um elemento com a mesma chave na coleção OrderedDictionary.

Exemplos

O exemplo de código a seguir demonstra a criação e a população de uma coleção OrderedDictionary . Esse código faz parte de um exemplo de código maior que pode ser exibido em 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)

Comentários

Uma chave não pode ser null, mas um valor pode ser.

Você também pode usar a Item[] propriedade para adicionar novos elementos definindo o valor de uma chave que não existe na OrderedDictionary coleção; no entanto, se a chave especificada já existir no OrderedDictionary, definir a Item[] propriedade substituirá o valor antigo. Por outro lado, o Add método não modifica elementos existentes ArgumentException, mas, em vez disso, lança .

Aplica-se a