
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Insert a new entry in OrderedDictionary with specified key and value in C#
To insert a new entry in OrderedDictionary with specified key and value, the code is as follows −
Example
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); } } }
Output
This will produce the following output −
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
Let us now see another example −
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("
Elements...UPDATED"); demoEnum = dict.GetEnumerator(); while (demoEnum.MoveNext()) { Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value); } dict.Insert(2, "K", "Speakers"); Console.WriteLine("
Elements...UPDATED"); demoEnum = dict.GetEnumerator(); while (demoEnum.MoveNext()) { Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value); } } }
Output
This will produce the following output −
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
- Related Articles
- Insert into OrderedDictionary with key and value at specified index in C#
- Remove entry with specified key from OrderedDictionary in C#
- Remove the entry at specified index from OrderedDictionary in C#
- Remove entry with specified key from the StringDictionary in C#
- Remove the entry with specified key from ListDictionary in C#
- Add key and value into OrderedDictionary collection in C#
- Removing the specified key entry from HybridDictionary in C#
- Gets or sets the value in HybridDictionary with specified key in C#
- Adding the specified key and value into HybridDictionary in C#
- Get or Set the value associated with specified key in Hashtable in C#
- Get or set the value associated with specified key in ListDictionary in C#
- Get or set the value associated with specified key in SortedList in C#
- Add the specified key and value into the ListDictionary in C#
- Insert a specified element in a specified position in JavaScript?
- Get or set the value associated with the specified key in StringDictionary in C#

Advertisements