C# में ListDictionary में कोई विशिष्ट कुंजी मौजूद है या नहीं यह जांचने के लिए, हम Contains मेथड का उपयोग करते हैं। यह मेथड निर्दिष्ट कुंजी मिलने पर true और न मिलने पर false लौटाता है। ListDictionary छोटे संग्रह के लिए कुशल है और System.Collections.Specialized नेमस्पेस में स्थित है।
उदाहरण 1: दो डिक्शनरी में कुंजी की उपस्थिति जांचना
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo
{
public static void Main()
{
ListDictionary dict1 = new ListDictionary();
dict1.Add("A", "Books");
dict1.Add("B", "Electronics");
dict1.Add("C", "Smart Wearables");
dict1.Add("D", "Pet Supplies");
dict1.Add("E", "Clothing");
dict1.Add("F", "Footwear");
Console.WriteLine("ListDictionary1 तत्व...");
foreach (DictionaryEntry d in dict1)
{
Console.WriteLine(d.Key + " " + d.Value);
}
ListDictionary dict2 = new ListDictionary();
dict2.Add("1", "One");
dict2.Add("2", "Two");
dict2.Add("3", "Three");
dict2.Add("4", "Four");
dict2.Add("5", "Five");
dict2.Add("6", "Six");
Console.WriteLine("\nListDictionary2 कुंजी-मान जोड़े...");
IDictionaryEnumerator demoEnum = dict2.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("कुंजी = " + demoEnum.Key + ", मान = " + demoEnum.Value);
Console.WriteLine("क्या ListDictionary का आकार निश्चित है? = " + dict2.IsFixedSize);
Console.WriteLine("क्या ListDictionary केवल-पढ़ने योग्य है? = " + dict2.IsReadOnly);
Console.WriteLine("क्या ListDictionary सिंक्रनाइज़्ड है? = " + dict2.IsSynchronized);
Console.WriteLine("क्या ListDictionary में कुंजी 5 है? = " + dict2.Contains("5"));
}
}
आउटपुट
ListDictionary1 तत्व... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear ListDictionary2 कुंजी-मान जोड़े... कुंजी = 1, मान = One कुंजी = 2, मान = Two कुंजी = 3, मान = Three कुंजी = 4, मान = Four कुंजी = 5, मान = Five कुंजी = 6, मान = Six क्या ListDictionary का आकार निश्चित है? = False क्या ListDictionary केवल-पढ़ने योग्य है? = False क्या ListDictionary सिंक्रनाइज़्ड है? = False क्या ListDictionary में कुंजी 5 है? = True
उदाहरण 2: अस्तित्वहीन कुंजी की जांच
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo
{
public static void Main()
{
ListDictionary dict = new ListDictionary();
dict.Add("1", "SUV");
dict.Add("2", "Sedan");
dict.Add("3", "Utility Vehicle");
dict.Add("4", "Compact Car");
dict.Add("5", "SUV");
dict.Add("6", "Sedan");
dict.Add("7", "Utility Vehicle");
dict.Add("8", "Compact Car");
dict.Add("9", "Crossover");
dict.Add("10", "Electric Car");
Console.WriteLine("ListDictionary तत्व...");
foreach (DictionaryEntry d in dict)
{
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("\nक्या ListDictionary का आकार निश्चित है? = " + dict.IsFixedSize);
Console.WriteLine("क्या ListDictionary केवल-पढ़ने योग्य है? = " + dict.IsReadOnly);
Console.WriteLine("क्या ListDictionary सिंक्रनाइज़्ड है? = " + dict.IsSynchronized);
Console.WriteLine("क्या ListDictionary में कुंजी M है? = " + dict.Contains("M"));
Console.WriteLine("क्या ListDictionary में कुंजी 9 है? = " + dict.Contains("9"));
}
}
आउटपुट
ListDictionary तत्व... 1 SUV 2 Sedan 3 Utility Vehicle 4 Compact Car 5 SUV 6 Sedan 7 Utility Vehicle 8 Compact Car 9 Crossover 10 Electric Car क्या ListDictionary का आकार निश्चित है? = False क्या ListDictionary केवल-पढ़ने योग्य है? = False क्या ListDictionary सिंक्रनाइज़्ड है? = False क्या ListDictionary में कुंजी M है? = False क्या ListDictionary में कुंजी 9 है? = True
नोट: Contains मेथड केस-सेंसिटिव है। पहले उदाहरण में कुंजी "5" (स्ट्रिंग) मौजूद है, इसलिए यह true लौटाता है। दूसरे उदाहरण में कुंजी "M" मौजूद नहीं है, इसलिए यह false लौटाता है, जबकि कुंजी "9" मौजूद होने पर true लौटाता है।