उदाहरण 1: दो StringDictionary ऑब्जेक्ट्स पर पुनरावृति
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict1 = new StringDictionary();
strDict1.Add("A", "John");
strDict1.Add("B", "Andy");
strDict1.Add("C", "Tim");
strDict1.Add("D", "Ryan");
strDict1.Add("E", "Kevin");
strDict1.Add("F", "Katie");
strDict1.Add("G", "Brad");
Console.WriteLine("StringDictionary1 तत्व...");
foreach(DictionaryEntry de in strDict1) {
Console.WriteLine(de.Key + " " + de.Value);
}
StringDictionary strDict2 = new StringDictionary();
strDict2.Add("1", "A");
strDict2.Add("2", "B");
strDict2.Add("3", "C");
strDict2.Add("4", "D");
strDict2.Add("5", "E");
Console.WriteLine("\nStringDictionary2 कुंजी-मान जोड़े...");
IEnumerator demoEnum = strDict2.GetEnumerator();
DictionaryEntry d;
while (demoEnum.MoveNext()) {
d = (DictionaryEntry)demoEnum.Current;
Console.WriteLine("कुंजी = " + d.Key + ", मान = " + d.Value);
}
}
}
आउटपुट
StringDictionary1 तत्व...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
StringDictionary2 कुंजी-मान जोड़े...
कुंजी = 1, मान = A
कुंजी = 2, मान = B
कुंजी = 3, मान = C
कुंजी = 4, मान = D
कुंजी = 5, मान = E
उदाहरण 2: एन्यूमरेटर का उपयोग करके पुनरावृति
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict = new StringDictionary();
strDict.Add("1", "One");
strDict.Add("2", "Two");
strDict.Add("3", "Three");
strDict.Add("4", "Four");
Console.WriteLine("StringDictionary कुंजी-मान जोड़े...");
IEnumerator demoEnum = strDict.GetEnumerator();
DictionaryEntry d;
while (demoEnum.MoveNext()) {
d = (DictionaryEntry)demoEnum.Current;
Console.WriteLine("कुंजी = " + d.Key + ", मान = " + d.Value);
}
}
}
आउटपुट
StringDictionary कुंजी-मान जोड़े...
कुंजी = 1, मान = One
कुंजी = 2, मान = Two
कुंजी = 3, मान = Three
कुंजी = 4, मान = Four