Dictionary<TKey, TValue>.ContainsValue() मेथड का उपयोग करके जांचें कि डिक्शनरी में विशिष्ट वैल्यू मौजूद है या नहीं।
सिंटैक्स
public bool ContainsValue(TValue value);
उदाहरण 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("Count of elements = " + dict.Count);
Console.WriteLine("\nKey/value pairs...");
foreach (KeyValuePair<string, string> res in dict) {
Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
}
if (dict.ContainsValue("Kevin")) {
Console.WriteLine("Value found!");
} else {
Console.WriteLine("Value isn't in the dictionary!");
}
dict.Clear();
Console.WriteLine("Cleared Key/value pairs...");
Console.WriteLine("Count of elements now = " + dict.Count);
}
}
आउटपुट
Count of elements = 5
Key/value pairs...
Key = One, Value = John
Key = Two, Value = Tom
Key = Three, Value = Jacob
Key = Four, Value = Kevin
Key = Five, Value = Nathan
Value found!
Cleared Key/value pairs...
Count of elements now = 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", "Chris");
dict.Add("Two", "Steve");
dict.Add("Three", "Messi");
dict.Add("Four", "Ryan");
dict.Add("Five", "Nathan");
Console.WriteLine("Count of elements = " + dict.Count);
Console.WriteLine("\nKey/value pairs...");
foreach (KeyValuePair<string, string> res in dict) {
Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
}
if (dict.ContainsValue("Angelina")) {
Console.WriteLine("Value found!");
} else {
Console.WriteLine("Value isn't in the dictionary!");
}
dict.Clear();
Console.WriteLine("Cleared Key/value pairs...");
Console.WriteLine("Count of elements now = " + dict.Count);
}
}
आउटपुट
Count of elements = 5
Key/value pairs...
Key = One, Value = Chris
Key = Two, Value = Steve
Key = Three, Value = Messi
Key = Four, Value = Ryan
Key = Five, Value = Nathan
Value isn't in the dictionary!
Cleared Key/value pairs...
Count of elements now = 0
महत्वपूर्ण बिंदु
- ContainsValue O(n) है - सभी वैल्यूज को लीनियरली खोजता है
- ContainsKey O(1) है - हैश लुकअप का उपयोग करता है
- यदि की ज्ञात है, तो
ContainsKeyयाTryGetValueप्राथमिकता दें - डुप्लीकेट वैल्यूज अनुमत हैं - पहला मिलान काफी है
- नल वैल्यू के लिए भी काम करता है
संबंधित मेथड्स
| मेथड | विवरण | कॉम्प्लेक्सिटी |
|---|---|---|
ContainsKey(key) | की मौजूद है? | O(1) |
ContainsValue(value) | वैल्यू मौजूद है? | O(n) |
TryGetValue(key, out value) | की से वैल्यू प्राप्त करें | O(1) |
this[key] | इंडेक्सर (की मौजूद न होने पर एक्सेप्शन) | O(1) |
बेस्ट प्रैक्टिस: TryGetValue पैटर्न
// खराब: दो लुकअप्स
if (dict.ContainsKey(key)) {
var value = dict[key];
}
// अच्छा: एक लुकअप
if (dict.TryGetValue(key, out var value)) {
// value उपयोग करें
}
// केवल वैल्यू चेक के लिए (की अनजान)
if (dict.ContainsValue(targetValue)) { ... }
LINQ विकल्प
// किसी भी वैल्यू के लिए प्रेडिकेट
bool hasLongName = dict.Values.Any(v => v.Length > 10);
// पहली मिलान वाली की ढूंढें
var key = dict.FirstOrDefault(kvp => kvp.Value == "Kevin").Key;