কম্পিউটার টিউটোরিয়াল

C#-এ SortedList-এ নির্দিষ্ট মান আছে কিনা যাচাই করা (ContainsValue)


C#-এ SortedList.ContainsValue() মেথডটি একটি SortedList-এ নির্দিষ্ট মান (value) আছে কিনা তা যাচাই করতে ব্যবহৃত হয়। SortedList একটি সর্টেড কি-ভ্যালু পেয়ার কালেকশন যা কি (key) অনুযায়ী সর্টেড থাকে।

উদাহরণ ১: মান উপস্থিত থাকা পরিস্থিতি

using System; 
using System.Collections; 
public class Demo { 
    public static void Main(){ 
        SortedList list = new SortedList(); 
        list.Add("1", "One"); 
        list.Add("2", "Two"); 
        list.Add("3", "Three"); 
        list.Add("4", "Four"); 
        list.Add("5", "Five"); 
        list.Add("6", "Six"); 
        list.Add("7", "Seven"); 
        list.Add("8", "Eight"); 
        Console.WriteLine("Key and Value of SortedList...."); 
        foreach(DictionaryEntry k in list ) 
            Console.WriteLine("Key: {0}, Value: {1}", k.Key , k.Value ); 
        Console.WriteLine("Is the SortedList having the value? " + list.ContainsValue("Five")); 
    } 
}

আউটপুট

Key and Value of SortedList.... 
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 
Is the SortedList having the value? True

উদাহরণ ২: মান অনুপস্থিত থাকা পরিস্থিতি

using System; 
using System.Collections; 
public class Demo { 
    public static void Main(){ 
        SortedList list = new SortedList(); 
        list.Add("1", "One"); 
        list.Add("2", "Two"); 
        list.Add("3", "Three"); 
        list.Add("4", "Four"); 
        list.Add("5", "Five"); 
        list.Add("6", "Six"); 
        list.Add("7", "Seven"); 
        list.Add("8", "Eight"); 
        Console.WriteLine("Key and Value of SortedList...."); 
        foreach(DictionaryEntry k in list ) 
            Console.WriteLine("Key: {0}, Value: {1}", k.Key , k.Value ); 
        Console.WriteLine("Is the SortedList having the value? " + list.ContainsValue("Tenth")); 
    } 
}

আউটপুট

Key and Value of SortedList.... 
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 
Is the SortedList having the value? False

মূল পয়েন্টস

  • ContainsValue() মেথড লিনিয়ার সার্চ (linear search) করে - O(n) সময় জটিলতা।
  • ContainsKey() মেথড কি (key) খুঁজে বের করতে ব্যবহৃত হয় - O(log n) বাইনারি সার্চ ব্যবহার করে।
  • একই মান একাধিক কির সাথে থাকতে পারে, প্রথম মিল পাওয়ায় true রিটার্ন করে।
  • নাল (null) মানও খুঁজে বের করা যায়।
  • জেনেরিক সংস্করণ SortedList<TKey, TValue> টাইপ সেফটি প্রদান করে।
  1. C#-এ StringDictionary-তে নির্দিষ্ট কী রয়েছে কিনা তা যাচাই করুন

  2. C#-এ Hashtable-তে নির্দিষ্ট কী রয়েছে কিনা তা যাচাই করুন

  3. C#-এ SortedSet-তে নির্দিষ্ট উপাদান রয়েছে কিনা তা যাচাই করুন

  4. C#-এ Hashtable-তে নির্দিষ্ট মান রয়েছে কিনা তা যাচাই করুন