उदाहरण 1: OrderedDictionary में Insert मेथड का उपयोग
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("तत्व...");
IDictionaryEnumerator demoEnum = dict.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine("कुंजी = " + demoEnum.Key + ", मान = " + demoEnum.Value);
}
dict.Insert(7, "15", "Fifteen");
Console.WriteLine("तत्व... अपडेटेड");
demoEnum = dict.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine("कुंजी = " + demoEnum.Key + ", मान = " + demoEnum.Value);
}
}
}
आउटपुट
तत्व...
कुंजी = 1, मान = One
कुंजी = 2, मान = Two
कुंजी = 3, मान = Three
कुंजी = 4, मान = Four
कुंजी = 5, मान = Five
कुंजी = 6, मान = Six
कुंजी = 7, मान = Seven
कुंजी = 8, मान = Eight
तत्व... अपडेटेड
कुंजी = 1, मान = One
कुंजी = 2, मान = Two
कुंजी = 3, मान = Three
कुंजी = 4, मान = Four
कुंजी = 5, मान = Five
कुंजी = 6, मान = Six
कुंजी = 7, मान = Seven
कुंजी = 15, मान = Fifteen
कुंजी = 8, मान = Eight
उदाहरण 2: Add और Insert का संयुक्त उपयोग
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("तत्व...");
IDictionaryEnumerator demoEnum = dict.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine("कुंजी = " + demoEnum.Key + ", मान = " + demoEnum.Value);
}
dict.Add("C", "Ultrabook");
dict.Add("D", "Alienware");
Console.WriteLine("\nतत्व... अपडेटेड");
demoEnum = dict.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine("कुंजी = " + demoEnum.Key + ", मान = " + demoEnum.Value);
}
dict.Insert(2, "K", "Speakers");
Console.WriteLine("\nतत्व... अपडेटेड");
demoEnum = dict.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine("कुंजी = " + demoEnum.Key + ", मान = " + demoEnum.Value);
}
}
}
आउटपुट
तत्व...
कुंजी = A, मान = Laptop
कुंजी = B, मान = Desktop
तत्व... अपडेटेड
कुंजी = A, मान = Laptop
कुंजी = B, मान = Desktop
कुंजी = C, मान = Ultrabook
कुंजी = D, मान = Alienware
तत्व... अपडेटेड
कुंजी = A, मान = Laptop
कुंजी = B, मान = Desktop
कुंजी = K, मान = Speakers
कुंजी = C, मान = Ultrabook
कुंजी = D, मान = Alienware