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

C#-এ দুটি Hashtable সমান কিনা যাচাই করা (Equals মেথড)


C#-এ Hashtable.Equals() মেথডটি দুটি Hashtable অবজেক্ট একই কিনা তা যাচাই করতে ব্যবহৃত হয়। এটি রেফারেন্স সমতা (reference equality) চেক করে, কন্টেন্ট সমতা (content equality) নয়। দুটি Hashtable একই কি-ভ্যালু পেয়ার রাখলেও আলাদা অবজেক্ট হলে false রিটার্ন করবে।

উদাহরণ ১: ভিন্ন কন্টেন্টवाले দুটি Hashtable

using System; 
using System.Collections; 
public class Demo { 
    public static void Main(){ 
        Hashtable hash1 = new Hashtable(); 
        hash1.Add("1", "Kevin"); 
        hash1.Add("2", "Steve"); 
        hash1.Add("3", "Tim"); 
        hash1.Add("4", "Gary"); 
        hash1.Add("5", "Kevin"); 
        hash1.Add("6", "Steve"); 
        hash1.Add("7", "Tom"); 
        hash1.Add("8", "Stephen"); 
        Console.WriteLine("HashSet1..."); 
        ICollection key = hash1.Keys; 
        foreach (string k in key) { 
            Console.WriteLine(k + ": " + hash1[k]); 
        } 
        Hashtable hash2 = new Hashtable(); 
        hash2.Add("1", "Kevin"); 
        hash2.Add("2", "Steve"); 
        hash2.Add("3", "John"); 
        hash2.Add("4", "Tim"); 
        key = hash2.Keys; 
        Console.WriteLine("\nHashSet2..."); 
        foreach (string k in key) { 
            Console.WriteLine(k + ": " + hash2[k]); 
        } 
        Console.WriteLine("\nAre both the Hashtable equal? " + (hash1.Equals(hash2))); 
    } 
}

আউটপুট

HashSet1... 
1: Kevin 
2: Steve 
3: Tim 
4: Gary 
5: Kevin 
6: Steve 
7: Tom 
8: Stephen 
HashSet2... 
1: Kevin 
2: Steve 
3: John 
4: Tim 
Are both the Hashtable equal? False

উদাহরণ ২: আংশিক মিলवाले দুটি Hashtable

using System; 
using System.Collections; 
public class Demo { 
    public static void Main(){ 
        Hashtable hash1 = new Hashtable(); 
        hash1.Add("1", "Kevin"); 
        hash1.Add("2", "Steve"); 
        hash1.Add("3", "John"); 
        hash1.Add("4", "Tim"); 
        Console.WriteLine("HashSet1..."); 
        ICollection key = hash1.Keys; 
        foreach (string k in key) { 
            Console.WriteLine(k + ": " + hash1[k]); 
        } 
        Hashtable hash2 = new Hashtable(); 
        hash2.Add("1", "Nathan"); 
        hash2.Add("2", "Gary"); 
        hash2.Add("3", "John"); 
        hash2.Add("4", "Tim"); 
        hash2.Add("5", "Steve"); 
        ICollection key2 = hash2.Keys; 
        Console.WriteLine("\nHashSet2..."); 
        foreach (string k in key2) { 
            Console.WriteLine(k + ": " + hash2[k]); 
        } 
        Console.WriteLine("\nAre both the Hashtable equal? " + (hash1.Equals(hash2))); 
    } 
}

আউটপুট

HashSet1... 
1: Kevin 
2: Steve 
3: John 
4: Tim 
HashSet2... 
1: Nathan 
2: Gary 
3: John 
4: Tim 
5: Steve 
Are both the Hashtable equal? False

কন্টেন্ট সমতা (Content Equality) চেক করার উপায়

যেহেতু Equals() রেফারেন্স সমতা চেক করে, কন্টেন্ট সমতা চেক করতে নিচের পদ্ধতিগুলো ব্যবহার করুন:

// পদ্ধতি ১: ম্যানুয়াল তুলনা
bool AreEqual(Hashtable h1, Hashtable h2) {
    if (h1.Count != h2.Count) return false;
    foreach (DictionaryEntry entry in h1) {
        if (!h2.ContainsKey(entry.Key)) return false;
        if (!Equals(h2[entry.Key], entry.Value)) return false;
    }
    return true;
}

// পদ্ধতি ২: LINQ ব্যবহার করে (System.Linq প্রয়োজন)
bool AreEqualLinq(Hashtable h1, Hashtable h2) {
    return h1.Count == h2.Count && 
           h1.Keys.Cast<object>().All(k => h2.ContainsKey(k) && Equals(h1[k], h2[k]));
}

মূল পয়েন্টস

  • Hashtable.Equals() ডিফল্টভাবে রেফারেন্স সমতা চেক করে (Object.Equals-এর মতো)।
  • একই কন্টেন্টवाले দুটি আলাদা Hashtable অবজেক্ট false রিটার্ন করবে।
  • কন্টেন্ট তুলনার জন্য কাস্টম লজিক বা LINQ ব্যবহার করতে হয়।
  • Dictionary<TKey, TValue>-ও একই আচরণ করে (রেফারেন্স সমতা)।
  • গভীর তুলনা (deep equality) এর জন্য সিকোয়েন্স ইকুয়ালিটি চেক করুন।
  1. C#-এ দুটি SortedSet অবজেক্ট সমান কিনা তা যাচাই করুন

  2. C#-এ Hashtable রিড-অনলি কিনা তা যাচাই করুন

  3. C#-এ Hashtable-র ফিক্সড সাইজ কিনা তা যাচাই করুন

  4. C#-এ একটি অ্যারে অবজেক্ট অন্য অ্যারে অবজেক্টের সমান কিনা তা যাচাই করুন