सबसे पहले, तत्वों के साथ एक शब्दकोश संग्रह सेट करें।
Dictionary<int, string> d = new Dictionary<int, string>() {
{1,"Applianes"},
{2, "Clothing"},
{3,"Toys"},
{4,"Footwear"},
{5, "Accessories"}
}; अब, मान लें कि आपको यह जांचने की आवश्यकता है कि कुंजी 5 मौजूद है या नहीं। उसके लिए, कंटेन्सकी () विधि का उपयोग करें। कुंजी मिलने पर यह सही हो जाता है।
d.ContainsKey(5);
आइए देखें पूरा कोड।
उदाहरण
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
Dictionary<int, string> d = new Dictionary<int, string>() {
{1,"Electronics"},
{2, "Clothing"},
{3,"Toys"},
{4,"Footwear"},
{5, "Accessories"}
};
foreach (KeyValuePair<int, string> ele in d) {
Console.WriteLine("Key = {0}, Value = {1}", ele.Key, ele.Value);
}
Console.WriteLine("Key 5 exists? "+d.ContainsKey(5));
}
} आउटपुट
Key = 1, Value = Electronics Key = 2, Value = Clothing Key = 3, Value = Toys Key = 4, Value = Footwear Key = 5, Value = Accessories Key 5 exists? True