OrderedDictionary.Contains() मेथड जांचता है कि निर्दिष्ट कुंजी डिक्शनरी में मौजूद है या नहीं। यह कुंजी-आधारित लुकअप है (वैल्यू-आधारित नहीं)। कुंजियों की तुलना केस-सेंसिटिव तरीके से की जाती है।
उदाहरण 1: संख्यात्मक स्ट्रिंग कुंजियों के साथ
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
OrderedDictionary dict = new OrderedDictionary();
dict.Add("1", "One");
dict.Add("2", "Two");
dict.Add("3", "Three");
dict.Add("4", "Four");
dict.Add("5", "Five");
dict.Add("6", "Six");
dict.Add("7", "Seven");
dict.Add("8", "Eight");
// वैल्यूज़ कॉपी करें
ICollection values = dict.Values;
string[] strValues = new string[dict.Count];
values.CopyTo(strValues, 0);
Console.WriteLine("वैल्यूज़ प्रदर्शित...");
for (int i = 0; i < dict.Count; i++) {
Console.WriteLine(strValues[i]);
}
// IsReadOnly जांचें
Console.WriteLine("\nक्या OrderedDictionary रीड-ओनली है? = " + dict.IsReadOnly);
// Contains - कुंजी मौजूदगी जांचें
Console.WriteLine("क्या कुंजी '15' मौजूद है? = " + dict.Contains("15"));
Console.WriteLine("क्या कुंजी '5' मौजूद है? = " + dict.Contains("5"));
}
}
आउटपुट
वैल्यूज़ प्रदर्शित...
One
Two
Three
Four
Five
Six
Seven
Eight
क्या OrderedDictionary रीड-ओनली है? = False
क्या कुंजी '15' मौजूद है? = False
क्या कुंजी '5' मौजूद है? = True
उदाहरण 2: वर्णमाला कुंजियों के साथ
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
OrderedDictionary dict = new OrderedDictionary();
dict.Add("A", "Pen");
dict.Add("B", "Pencil");
dict.Add("C", "Notebook");
dict.Add("D", "Table");
dict.Add("E", "Chair");
dict.Add("F", "Coworking");
dict.Add("G", "Cubicle");
dict.Add("H", "Sticky Notes");
dict.Add("I", "WhiteBoard");
dict.Add("J", "Marker");
ICollection values = dict.Values;
string[] strValues = new string[dict.Count];
values.CopyTo(strValues, 0);
Console.WriteLine("वैल्यूज़ प्रदर्शित...");
for (int i = 0; i < dict.Count; i++) {
Console.WriteLine(strValues[i]);
}
Console.WriteLine("\nक्या OrderedDictionary रीड-ओनली है? = " + dict.IsReadOnly);
Console.WriteLine("क्या कुंजी 'B' मौजूद है? = " + dict.Contains("B"));
Console.WriteLine("क्या कुंजी 'b' मौजूद है? = " + dict.Contains("b")); // केस-सेंसिटिव
Console.WriteLine("क्या कुंजी 'Z' मौजूद है? = " + dict.Contains("Z"));
}
}
आउटपुट
वैल्यूज़ प्रदर्शित...
Pen
Pencil
Notebook
Table
Chair
Coworking
Cubicle
Sticky Notes
WhiteBoard
Marker
क्या OrderedDictionary रीड-ओनली है? = False
क्या कुंजी 'B' मौजूद है? = True
क्या कुंजी 'b' मौजूद है? = False
क्या कुंजी 'Z' मौजूद है? = False
महत्वपूर्ण बिंदु
- केस-सेंसिटिव: "B" और "b" अलग कुंजियाँ हैं।
- केवल कुंजी: Contains केवल कुंजी जांचता है, वैल्यू नहीं।
- समय जटिलता: O(1) औसत (हैशटेबल-आधारित आंतरिक कार्यान्वयन)।
- नल कुंजी: नल कुंजी की अनुमति नहीं -
ArgumentNullExceptionफेंका जाता है।
Contains बनाम अन्य लुकअप मेथड्स
| मेथड | उद्देश्य | रिटर्न |
|---|---|---|
Contains(key) | कुंजी मौजूदगी जांचें | bool |
ContainsKey(key) | कुंजी मौजूदगी (IDictionary) | bool |
ContainsValue(value) | वैल्यू मौजूदगी जांचें | bool |
TryGetValue(key, out value) | कुंजी जांचें और वैल्यू प्राप्त करें | bool + आउट पैरामीटर |
आधुनिक विकल्प
// Dictionary<TKey, TValue> में (अनुशंसित)
var dict = new Dictionary<string, string>();
dict["A"] = "Pen";
bool hasKey = dict.ContainsKey("A"); // true
bool hasValue = dict.ContainsValue("Pen"); // true
// TryGetValue - सबसे कुशल (एक ही लुकअप में दोनों)
if (dict.TryGetValue("A", out string value)) {
Console.WriteLine("मिला: " + value);
}