C# में Dictionary.ContainsKey() मेथड जांचता है कि Dictionary<TKey, TValue> में निर्दिष्ट कुंजी है या नहीं।
सिंटैक्स
public bool ContainsKey (TKey key);
ऊपर, पैरामीटर key वह कुंजी है जिसे ढूंढा जाना है।
आइए अब Dictionary.ContainsKey() मेथड को लागू करने का एक उदाहरण देखें -
उदाहरण
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("One", "John");
dict.Add("Two", "Tom");
dict.Add("Three", "Jacob");
dict.Add("Four", "Kevin");
dict.Add("Five", "Nathan");
Console.WriteLine("तत्वों की संख्या = "+dict.Count);
Console.WriteLine("\nकुंजी/मान जोड़े...");
foreach(KeyValuePair<string, string> res in dict){
Console.WriteLine("कुंजी = {0}, मान = {1}", res.Key, res.Value);
}
if (dict.ContainsKey("Three"))
Console.WriteLine("कुंजी मिली!");
else
Console.WriteLine("कुंजी डिक्शनरी में नहीं है!");
dict.Clear();
Console.WriteLine("साफ़ किए गए कुंजी/मान जोड़े...");
foreach(KeyValuePair<string, string> res in dict){
Console.WriteLine("कुंजी = {0}, मान = {1}", res.Key, res.Value);
}
Console.WriteLine("अब तत्वों की संख्या = "+dict.Count);
}
}
आउटपुट
यह निम्नलिखित आउटपुट देगा -
तत्वों की संख्या = 5 कुंजी/मान जोड़े... कुंजी = One, मान = John कुंजी = Two, मान = Tom कुंजी = Three, मान = Jacob कुंजी = Four, मान = Kevin कुंजी = Five, मान = Nathan कुंजी मिली! साफ़ किए गए कुंजी/मान जोड़े... अब तत्वों की संख्या = 0
आइए अब Dictionary.ContainsKey() प्रॉपर्टी को लागू करने का एक और उदाहरण देखें -
उदाहरण
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("One", "John");
dict.Add("Two", "Tom");
dict.Add("Three", "Jacob");
dict.Add("Four", "Kevin");
dict.Add("Five", "Nathan");
Console.WriteLine("तत्वों की संख्या = "+dict.Count);
Console.WriteLine("\nकुंजी/मान जोड़े...");
foreach(KeyValuePair<string, string> res in dict){
Console.WriteLine("कुंजी = {0}, मान = {1}", res.Key, res.Value);
}
if (dict.ContainsKey("mykey"))
Console.WriteLine("कुंजी मिली!");
else
Console.WriteLine("कुंजी डिक्शनरी में नहीं है!");
dict.Clear();
Console.WriteLine("साफ़ किए गए कुंजी/मान जोड़े...");
foreach(KeyValuePair<string, string> res in dict){
Console.WriteLine("कुंजी = {0}, मान = {1}", res.Key, res.Value);
}
Console.WriteLine("अब तत्वों की संख्या = "+dict.Count);
}
}
आउटपुट
यह निम्नलिखित आउटपुट देगा -
तत्वों की संख्या = 5 कुंजी/मान जोड़े... कुंजी = One, मान = John कुंजी = Two, मान = Tom कुंजी = Three, मान = Jacob कुंजी = Four, मान = Kevin कुंजी = Five, मान = Nathan कुंजी डिक्शनरी में नहीं है! साफ़ किए गए कुंजी/मान जोड़े... अब तत्वों की संख्या = 0