C# में Dictionary.ContainsValue() मेथड का उपयोग यह जांचने के लिए किया जाता है कि Dictionary<TKey,TValue> में कोई विशिष्ट मान है या नहीं।
सिंटैक्स
public bool ContainsValue (TValue val);
ऊपर, val खोजा जाने वाला मान है।
आइए अब Dictionary.ContainsValue() मेथड को लागू करने का एक उदाहरण देखें -
उदाहरण
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.ContainsValue("Kevin"))
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.ContainsValue() मेथड को लागू करने का एक और उदाहरण देखें -
उदाहरण
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("तत्वों की संख्या = "+dict.Count);
Console.WriteLine("\nकुंजी/मान जोड़े...");
foreach(KeyValuePair<string, string> res in dict){
Console.WriteLine("कुंजी = {0}, मान = {1}", res.Key, res.Value);
}
if (dict.ContainsValue("Angelina"))
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, मान = Chris कुंजी = Two, मान = Steve कुंजी = Three, मान = Messi कुंजी = Four, मान = Ryan कुंजी = Five, मान = Nathan मान डिक्शनरी में नहीं है! साफ़ किए गए कुंजी/मान जोड़े... अब तत्वों की संख्या = 0