C# | Add key and value into OrderedDictionary collection

OrderedDictionary.Add(Object, Object) method is used to add an entry with the specified key and value into the OrderedDictionary collection with the lowest available index.

Syntax:

public void Add (object key, object value);

Parameters:



key : The key of the entry to add.
value : The value of the entry to add. This value can be null.

Exceptions :

  • NotSupportedException : If the OrderedDictionary collection is read-only.
  • ArgumentException : If an element with the same key already exists in the OrderedDictionary collection.

Below given are some examples to understand the implementation in a better way:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to add key and value
// into OrderedDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver method
    public static void Main()
    {
  
        // Creating a orderedDictionary named myDict
        OrderedDictionary myDict = new OrderedDictionary();
  
        // Adding key and value in myDict
        myDict.Add("key1", "value1");
        myDict.Add("key2", "value2");
        myDict.Add("key3", "value3");
        myDict.Add("key4", "value4");
        myDict.Add("key5", "value5");
  
        // Displaying the number of key/value
        // pairs in myDict
        Console.WriteLine(myDict.Count);
  
        // Displaying the key/value pairs in myDict
        foreach(DictionaryEntry de in myDict)
            Console.WriteLine(de.Key + " --> " + de.Value);
    }
}

chevron_right


Output:

5
key1 --> value1
key2 --> value2
key3 --> value3
key4 --> value4
key5 --> value5

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to add key and value
// into OrderedDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver method
    public static void Main()
    {
  
        // Creating a orderedDictionary named myDict
        OrderedDictionary myDict = new OrderedDictionary();
  
        // Adding key and value in myDict
        myDict.Add("key1", "value1");
        myDict.Add("key2", "value2");
  
        // This should raise "ArgumentException"
        // as an element with the same key already
        // exists in the OrderedDictionary collection.
        myDict.Add("key2", "value3");
        myDict.Add("key4", "value4");
        myDict.Add("key5", "value5");
  
        // Displaying the number of key/value
        // pairs in myDict
        Console.WriteLine(myDict.Count);
  
        // Displaying the key/value pairs in myDict
        foreach(DictionaryEntry de in myDict)
            Console.WriteLine(de.Key + " --> " + de.Value);
    }
}

chevron_right


Runtime Error:

Unhandled Exception:
System.ArgumentException: Item has already been added. Key in dictionary: ‘key2’ Key being added: ‘key2’

Note: A key cannot be null, but a value can be.

Reference:



My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.