Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Insert a new entry in OrderedDictionary with specified key and value in C#
The OrderedDictionary class in C# provides the Insert method to add a new entry at a specific position while maintaining the order of existing elements. This method allows you to specify both the index position and the key-value pair to insert.
Syntax
Following is the syntax for inserting an entry at a specific position in an OrderedDictionary −
orderedDictionary.Insert(index, key, value);
Parameters
-
index − The zero-based index at which to insert the key-value pair.
-
key − The key of the entry to insert.
-
value − The value of the entry to insert.
How It Works
When you call the Insert method, the new key-value pair is inserted at the specified index position. All existing entries at that position and beyond are shifted to higher indices to accommodate the new entry.
Using Insert Method
Example 1: Inserting at Specific Position
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
OrderedDictionary dict = new OrderedDictionary();
dict.Add("1", "One");
dict.Add("2", "Two");
dict.Add("3", "Three");
dict.Add("4", "Four");
dict.Add("5", "Five");
dict.Add("6", "Six");
dict.Add("7", "Seven");
dict.Add("8", "Eight");
Console.WriteLine("Elements...");
IDictionaryEnumerator demoEnum = dict.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
dict.Insert(7, "15", "Fifteen");
Console.WriteLine("Elements...UPDATED");
demoEnum = dict.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
}
}
The output of the above code is −
Elements... Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four Key = 5, Value = Five Key = 6, Value = Six Key = 7, Value = Seven Key = 8, Value = Eight Elements...UPDATED Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four Key = 5, Value = Five Key = 6, Value = Six Key = 7, Value = Seven Key = 15, Value = Fifteen Key = 8, Value = Eight
Example 2: Inserting in the Middle
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
OrderedDictionary dict = new OrderedDictionary();
dict.Add("A", "Laptop");
dict.Add("B", "Desktop");
Console.WriteLine("Elements...");
IDictionaryEnumerator demoEnum = dict.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
dict.Add("C", "Ultrabook");
dict.Add("D", "Alienware");
Console.WriteLine("\nElements...UPDATED");
demoEnum = dict.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
dict.Insert(2, "K", "Speakers");
Console.WriteLine("\nElements...UPDATED");
demoEnum = dict.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
}
}
The output of the above code is −
Elements... Key = A, Value = Laptop Key = B, Value = Desktop Elements...UPDATED Key = A, Value = Laptop Key = B, Value = Desktop Key = C, Value = Ultrabook Key = D, Value = Alienware Elements...UPDATED Key = A, Value = Laptop Key = B, Value = Desktop Key = K, Value = Speakers Key = C, Value = Ultrabook Key = D, Value = Alienware
Key Points
-
The
Insertmethod maintains the order of existing elements by shifting them to higher indices. -
The index parameter is zero-based, so index 0 represents the first position.
-
If the specified index is equal to the count of elements, the entry is added at the end.
-
An
ArgumentOutOfRangeExceptionis thrown if the index is less than zero or greater than the count.
Conclusion
The Insert method in OrderedDictionary allows you to add key-value pairs at specific positions while preserving the order of existing elements. This method is useful when you need to maintain a specific sequence of entries in your collection.
