Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C#

C# में SortedDictionary.ContainsValue() मेथड - मान की मौजूदगी जांचें

परिचय

C# में SortedDictionary.ContainsValue() मेथड यह निर्धारित करता है कि SortedDictionary<TKey, TValue> में निर्दिष्ट मान वाला कोई तत्व है या नहीं।

सिंटैक्स

public bool ContainsValue(TValue value);

value SortedDictionary<TKey, TValue> में खोजा जाने वाला मान है।

उदाहरण 1: मौजूद मान खोजना

using System;
using System.Collections;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        SortedDictionary sortedDict = new SortedDictionary();

        sortedDict.Add(100, "Inspiron");
        sortedDict.Add(200, "Alienware");
        sortedDict.Add(300, "Projectors");
        sortedDict.Add(400, "XPS");

        Console.WriteLine("SortedDictionary कुंजी-मान जोड़े...");
        IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext())
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);

        Console.WriteLine("SortedDictionary कुंजी-मान जोड़ों की संख्या = " + sortedDict.Count);

        sortedDict.Add(800, "Notebook");
        sortedDict.Add(10000, "Bluetooth Speaker");

        Console.WriteLine("\nSortedDictionary कुंजी-मान जोड़े...अपडेटेड");
        demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext())
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);

        Console.WriteLine("SortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = " + sortedDict.Count);

        Console.WriteLine("क्या मान SortedDictionary में है? = " + sortedDict.ContainsValue("Notebook"));

        sortedDict.Clear();
        Console.WriteLine("\nSortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = " + sortedDict.Count);
    }
}

आउटपुट

SortedDictionary कुंजी-मान जोड़े...
Key = 100, Value = Inspiron
Key = 200, Value = Alienware
Key = 300, Value = Projectors
Key = 400, Value = XPS
SortedDictionary कुंजी-मान जोड़ों की संख्या = 4

SortedDictionary कुंजी-मान जोड़े...अपडेटेड
Key = 100, Value = Inspiron
Key = 200, Value = Alienware
Key = 300, Value = Projectors
Key = 400, Value = XPS
Key = 800, Value = Notebook
Key = 10000, Value = Bluetooth Speaker
SortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = 6

क्या मान SortedDictionary में है? = True

SortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = 0

उदाहरण 2: अनुपस्थित मान खोजना

using System;
using System.Collections;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        SortedDictionary sortedDict = new SortedDictionary();

        sortedDict.Add(1, "One");
        sortedDict.Add(2, "Two");
        sortedDict.Add(3, "Three");
        sortedDict.Add(4, "Four");
        sortedDict.Add(5, "Five");
        sortedDict.Add(6, "Six");

        Console.WriteLine("SortedDictionary कुंजी-मान जोड़े...");
        IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext())
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);

        Console.WriteLine("SortedDictionary कुंजी-मान जोड़ों की संख्या = " + sortedDict.Count);

        sortedDict.Add(7, "Seven");
        sortedDict.Add(8, "Eight");

        Console.WriteLine("\nSortedDictionary कुंजी-मान जोड़े...अपडेटेड");
        demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext())
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);

        Console.WriteLine("SortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = " + sortedDict.Count);

        Console.WriteLine("क्या मान SortedDictionary में है? = " + sortedDict.ContainsValue("Eleven"));

        sortedDict.Clear();
        Console.WriteLine("\nSortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = " + sortedDict.Count);
    }
}

आउटपुट

SortedDictionary कुंजी-मान जोड़े...
Key = 1, Value = One
Key = 2, Value = Two
Key = 3, Value = Three
Key = 4, Value = Four
Key = 5, Value = Five
Key = 6, Value = Six
SortedDictionary कुंजी-मान जोड़ों की संख्या = 6

SortedDictionary कुंजी-मान जोड़े...अपडेटेड
Key = 1, Value = One
Key = 2, Value = Two
Key = 3, Value = Three
Key = 4, Value = Four
Key = 5, Value = Five
Key = 6, Value = Six
Key = 7, Value = Seven
Key = 8, Value = Eight
SortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = 8

क्या मान SortedDictionary में है? = False

SortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = 0

मुख्य बिंदु

  • ContainsValue() रैखिक खोज (linear search) करता है - O(n) समय जटिलता।
  • ContainsKey() O(log n) है (बाइनरी सर्च ट्री) - कुंजी द्वारा खोज बहुत तेज है।
  • मान डुप्लीकेट हो सकते हैं - पहला मिलान पर्याप्त है।
  • null मान के लिए भी काम करता है यदि TValue रेफरेंस टाइप या नलेएबल है।
  • केस-सेंसिटिव स्ट्रिंग तुलना के लिए डिफ़ॉल्ट तुलनाकर्ता का उपयोग करता है।
  1. - C# में Char.IsSymbol() मेथड

    C# में Char.IsSymbol() मेथड दर्शाता है कि किसी निर्दिष्ट स्ट्रिंग में निर्दिष्ट स्थिति पर वर्ण एक प्रतीक वर्ण के रूप में वर्गीकृत है या नहीं। सिंटैक्स public static bool IsSymbol (string str, int index); ऊपर, str एक स्ट्रिंग है, जबकि str में मूल्यांकन करने के लिए वर्ण की स्थिति। आइए अब Char.IsSymbo

  1. - C# में Dictionary.ContainsValue() मेथड

    C# में Dictionary.ContainsValue() मेथड का उपयोग यह जांचने के लिए किया जाता है कि Dictionary<TKey,TValue> में कोई विशिष्ट मान है या नहीं। सिंटैक्स public bool ContainsValue (TValue val); ऊपर, val खोजा जाने वाला मान है। आइए अब Dictionary.ContainsValue() मेथड को लागू करने का एक उदाहरण देखें - उद

  1. - C# में Convert.ToDouble विधि

    निर्दिष्ट मान को डबल-प्रिसिजन फ्लोटिंग-पॉइंट संख्या में परिवर्तित करने के लिए Convert.ToDouble() विधि का उपयोग करें। उदाहरण निम्न हमारा लंबा मान है: long[] val = { 340, -200 }; अब इसे Double में बदलें: double result; result = Convert.ToDouble(val); पूर्ण कोड using System; public class Demo { pu