C# | Insert a new entry in OrderedDictionary with specified key and value
OrderedDictionary.Insert(Int32, Object, Object) Method is used to inserts a new entry into the OrderedDictionary collection with the specified key and value at the specified index.
Syntax:
public void Insert (int index, object key, object value);
Parameters:
index: It is the zero-based index of type System.Int32 at which the element should be inserted.
key: It is the key of the entry to add.
value: It is the value of the entry to add. The value can be null.
Exceptions:
- ArgumentOutOfRangeException: If the index is out of range.
- NotSupportedException: If the collection is read-only.
Below programs illustrate the use of above-discussed method:
Example 1:
// C# code to add a new entry into the // OrderedDictionary collection with // the specified key and value at the // specified index. 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"); Console.WriteLine("Before Updation: "); // Displaying the key/value pairs in myDict foreach(DictionaryEntry de in myDict) Console.WriteLine(de.Key + " --> " + de.Value); // using Insert(Int32, Object, Object) Method // to add a new entry at index 5 myDict.Insert(5, "key6", "value6"); Console.WriteLine("\nAfter Updation: "); // Displaying the key/value pairs in myDict foreach(DictionaryEntry de in myDict) Console.WriteLine(de.Key + " --> " + de.Value); } } |
Output:
Before Updation: key1 --> value1 key2 --> value2 key3 --> value3 key4 --> value4 key5 --> value5 After Updation: key1 --> value1 key2 --> value2 key3 --> value3 key4 --> value4 key5 --> value5 key6 --> value6
Example 2:
// C# code to add a new entry into the // OrderedDictionary collection with // the specified key and value at the // specified index. 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("1", "C"); myDict.Add("2", "C++"); myDict.Add("3", "Java"); myDict.Add("4", "Python"); myDict.Add("5", "C#"); Console.WriteLine("Before Updation: "); // Displaying the key/value pairs in myDict foreach(DictionaryEntry de in myDict) Console.WriteLine(de.Key + " --> " + de.Value); // using Insert(Int32, Object, Object) Method // this will give error as index is out of range // here indexing is zero-based myDict.Insert(6, "6", "HTML"); Console.WriteLine("\nAfter Updation: "); // Displaying the key/value pairs in myDict foreach(DictionaryEntry de in myDict) Console.WriteLine(de.Key + " --> " + de.Value); } } |
Runtime Error:
Unhandled Exception:
System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index
Note:
- The key and value parameters will be appended to the end of the collection if the index parameter is equal to the number of entries in the OrderedDictionary collection.
- Entries that follow the insertion point move down to accommodate the new entry and the indexes of the moved entries are also updated.
Reference:
Recommended Posts:
- C# | Remove entry with specified key from OrderedDictionary
- C# | Remove the entry at specified index from OrderedDictionary
- C# | Insert into OrderedDictionary with key and value at specified index
- C# | Removing the specified key entry from HybridDictionary
- C# | Remove the entry with specified key from ListDictionary
- C# | Remove entry with specified key from the StringDictionary
- How to create an OrderedDictionary in C#
- C# | OrderedDictionary Class
- C# | Add key and value into OrderedDictionary collection
- C# | Get an IDictionaryEnumerator object in OrderedDictionary
- C# | Remove all elements from OrderedDictionary
- C# | Get an ICollection containing values in OrderedDictionary
- C# | Get an ICollection containing keys in OrderedDictionary
- C# | Get a read-only copy of the OrderedDictionary
- C# | Check if OrderedDictionary collection contains a specific key
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.



