Dictionary<TKey, TValue>.ContainsKey() मेथड जांचता है कि निर्दिष्ट कुंजी डिक्शनरी में मौजूद है या नहीं। यह O(1) औसत समय जटिलता के साथ हैश-आधारित लुकअप करता है।
उदाहरण 1: मौजूद कुंजी जांचना और क्लियर करना
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> pair in dict) {
Console.WriteLine("Key = {0}, Value = {1}", pair.Key, pair.Value);
}
// कुंजी मौजूदगी जांचें
if (dict.ContainsKey("Three")) {
Console.WriteLine("\nकुंजी मिली!");
} else {
Console.WriteLine("\nकुंजी डिक्शनरी में नहीं है!");
}
// सभी तत्व हटाएं
dict.Clear();
Console.WriteLine("क्लियर की गई की/वैल्यू पेयर्स...");
foreach (KeyValuePair<string, string> pair in dict) {
Console.WriteLine("Key = {0}, Value = {1}", pair.Key, pair.Value);
}
Console.WriteLine("अब तत्वों की संख्या = " + dict.Count);
}
}
आउटपुट
तत्वों की संख्या = 5
की/वैल्यू पेयर्स...
Key = One, Value = John
Key = Two, Value = Tom
Key = Three, Value = Jacob
Key = Four, Value = Kevin
Key = Five, Value = Nathan
कुंजी मिली!
क्लियर की गई की/वैल्यू पेयर्स...
अब तत्वों की संख्या = 0
उदाहरण 2: मौजूद न होने वाली कुंजी जांचना
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> pair in dict) {
Console.WriteLine("Key = {0}, Value = {1}", pair.Key, pair.Value);
}
// अस्तित्वहीन कुंजी जांचें
if (dict.ContainsKey("mykey")) {
Console.WriteLine("\nकुंजी मिली!");
} else {
Console.WriteLine("\nकुंजी डिक्शनरी में नहीं है!");
}
dict.Clear();
Console.WriteLine("क्लियर की गई की/वैल्यू पेयर्स...");
Console.WriteLine("अब तत्वों की संख्या = " + dict.Count);
}
}
आउटपुट
तत्वों की संख्या = 5
की/वैल्यू पेयर्स...
Key = One, Value = John
Key = Two, Value = Tom
Key = Three, Value = Jacob
Key = Four, Value = Kevin
Key = Five, Value = Nathan
कुंजी डिक्शनरी में नहीं है!
क्लियर की गई की/वैल्यू पेयर्स...
अब तत्वों की संख्या = 0
मुख्य मेथड्स तुलना
| मेथड | उद्देश्य | समय जटिलता | नोट |
|---|---|---|---|
ContainsKey(key) | कुंजी मौजूदगी जांचें | O(1) | सबसे सामान्य |
ContainsValue(value) | वैल्यू मौजूदगी जांचें | O(n) | रैखिक खोज |
TryGetValue(key, out value) | कुंजी जांचें + वैल्यू प्राप्त करें | O(1) | सबसे कुशल जब वैल्यू चाहिए |
this[key] (इंडेक्सर) | वैल्यू प्राप्त करें (एक्सेप्शन यदि नहीं) | O(1) | KeyNotFoundException संभव |
सर्वोत्तम अभ्यास: TryGetValue उपयोग करें
// दो लुकअप से बचें (ContainsKey + इंडेक्सर)
if (dict.ContainsKey(key)) {
string value = dict[key]; // दूसरा लुकअप!
}
// बेहतर: सिंगल लुकअप
if (dict.TryGetValue(key, out string value)) {
Console.WriteLine("मिला: " + value);
} else {
Console.WriteLine("नहीं मिला");
}
महत्वपूर्ण नोट्स
- नल कुंजी: रेफरेंस टाइप कुंजियों के लिए नल पास करने पर
ArgumentNullException। - कस्टम तुलना: कंस्ट्रक्टर में
IEqualityComparer<TKey>पास करें। - केस सेंसिटिविटी: स्ट्रिंग कुंजियों के लिए
StringComparer.OrdinalIgnoreCaseउपयोग करें। - थ्रेड सुरक्षा: Dictionary थ्रेड-सेफ नहीं; समवर्ती एक्सेस के लिए
ConcurrentDictionaryउपयोग करें।