C# Dictionary में कुंजी-मान जोड़ी जोड़ने के लिए, पहले Dictionary घोषित करें:
IDictionary<int, string> d = new Dictionary<int, string>();
अब KeyValuePair के साथ तत्व जोड़ें:
d.Add(new KeyValuePair<int, string>(1, "TVs")); d.Add(new KeyValuePair<int, string>(2, "Appliances")); d.Add(new KeyValuePair<int, string>(3, "Mobile"));
तत्व जोड़ने के बाद, कुंजी-मान जोड़ी प्रदर्शित करें:
पूर्ण कोड
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
IDictionary<int, string> d = new Dictionary<int, string>();
d.Add(new KeyValuePair<int, string>(1, "TVs"));
d.Add(new KeyValuePair<int, string>(2, "Appliances"));
d.Add(new KeyValuePair<int, string>(3, "Mobile"));
d.Add(new KeyValuePair<int, string>(4, "Tablet"));
d.Add(new KeyValuePair<int, string>(5, "Laptop"));
d.Add(new KeyValuePair<int, string>(6, "Desktop"));
d.Add(new KeyValuePair<int, string>(7, "Hard Drive"));
d.Add(new KeyValuePair<int, string>(8, "Flash Drive"));
foreach (KeyValuePair<int, string> ele in d) {
Console.WriteLine("कुंजी = {0}, मान = {1}", ele.Key, ele.Value);
}
}
}
आउटपुट
कुंजी = 1, मान = TVs कुंजी = 2, मान = Appliances कुंजी = 3, मान = Mobile कुंजी = 4, मान = Tablet कुंजी = 5, मान = Laptop कुंजी = 6, मान = Desktop कुंजी = 7, मान = Hard Drive कुंजी = 8, मान = Flash Drive