उदाहरण 1: Dictionary में तत्व जोड़ना और गिनना
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Dictionary dict = new Dictionary();
dict.Add("One", "Chris");
dict.Add("Two", "Steve");
dict.Add("Three", "Messi");
dict.Add("Four", "Ryan");
dict.Add("Five", "Nathan");
Console.WriteLine("तत्वों की संख्या = " + dict.Count);
Console.WriteLine("\nकुंजी/मान जोड़े...");
foreach(KeyValuePair res in dict) {
Console.WriteLine("कुंजी = {0}, मान = {1}", res.Key, res.Value);
}
if (dict.ContainsValue("Angelina"))
Console.WriteLine("मान मिला!");
else
Console.WriteLine("मान डिक्शनरी में नहीं है!");
dict.Clear();
Console.WriteLine("साफ किए गए कुंजी/मान जोड़े...");
foreach(KeyValuePair res in dict) {
Console.WriteLine("कुंजी = {0}, मान = {1}", res.Key, res.Value);
}
Console.WriteLine("अब तत्वों की संख्या = " + dict.Count);
}
}
आउटपुट
तत्वों की संख्या = 5
कुंजी/मान जोड़े...
कुंजी = One, मान = Chris
कुंजी = Two, मान = Steve
कुंजी = Three, मान = Messi
कुंजी = Four, मान = Ryan
कुंजी = Five, मान = Nathan
मान डिक्शनरी में नहीं है!
साफ किए गए कुंजी/मान जोड़े...
अब तत्वों की संख्या = 0
उदाहरण 2: सरल Dictionary गणना
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Dictionary dict = new Dictionary();
dict.Add("One", "David");
dict.Add("Two", "Brian");
dict.Add("Three", "Paul");
dict.Add("Four", "Ryan");
dict.Add("Five", "Nathan");
Console.WriteLine("तत्वों की संख्या = " + dict.Count);
}
}
आउटपुट
तत्वों की संख्या = 5