Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if two HybridDictionary objects are equal in C#
The HybridDictionary class in C# provides a collection that uses a ListDictionary for small collections and switches to a Hashtable for larger collections. To check if two HybridDictionary objects are equal, you can use the Equals() method, which performs reference equality comparison by default.
Syntax
Following is the syntax for checking equality between HybridDictionary objects −
bool isEqual = dict1.Equals(dict2);
For content-based comparison, you need to implement custom logic −
public static bool AreEqual(HybridDictionary dict1, HybridDictionary dict2) {
if (dict1.Count != dict2.Count) return false;
foreach (DictionaryEntry entry in dict1) {
if (!dict2.Contains(entry.Key) || !dict2[entry.Key].Equals(entry.Value))
return false;
}
return true;
}
Using Reference Equality (Same Object)
When two HybridDictionary variables reference the same object in memory, Equals() returns true −
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
HybridDictionary dict1 = new HybridDictionary();
dict1.Add("A", "Books");
dict1.Add("B", "Electronics");
dict1.Add("C", "Smart Wearables");
dict1.Add("D", "Pet Supplies");
dict1.Add("E", "Clothing");
dict1.Add("F", "Footwear");
Console.WriteLine("HybridDictionary1 elements...");
foreach(DictionaryEntry d in dict1) {
Console.WriteLine(d.Key + " " + d.Value);
}
HybridDictionary dict2 = new HybridDictionary();
dict2.Add("1", "One");
dict2.Add("2", "Two");
dict2.Add("3", "Three");
dict2.Add("4", "Four");
dict2.Add("5", "Five");
dict2.Add("6", "Six");
Console.WriteLine("\nHybridDictionary2 elements...");
foreach(DictionaryEntry d in dict2) {
Console.WriteLine(d.Key + " " + d.Value);
}
HybridDictionary dict3 = dict2;
Console.WriteLine("\nIs HybridDictionary3 equal to HybridDictionary2? = " + (dict3.Equals(dict2)));
}
}
The output of the above code is −
HybridDictionary1 elements... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear HybridDictionary2 elements... 1 One 2 Two 3 Three 4 Four 5 Five 6 Six Is HybridDictionary3 equal to HybridDictionary2? = True
Using Reference Equality (Different Objects)
When two HybridDictionary objects are different instances, even with identical content, Equals() returns false −
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
HybridDictionary dict1 = new HybridDictionary();
dict1.Add("A", "Books");
dict1.Add("B", "Electronics");
dict1.Add("C", "Smart Wearables");
dict1.Add("D", "Pet Supplies");
dict1.Add("E", "Clothing");
dict1.Add("F", "Footwear");
Console.WriteLine("HybridDictionary1 elements...");
foreach(DictionaryEntry d in dict1) {
Console.WriteLine(d.Key + " " + d.Value);
}
HybridDictionary dict2 = new HybridDictionary();
dict2.Add("1", "One");
dict2.Add("2", "Two");
dict2.Add("3", "Three");
dict2.Add("4", "Four");
dict2.Add("5", "Five");
dict2.Add("6", "Six");
Console.WriteLine("\nHybridDictionary2 elements...");
foreach(DictionaryEntry d in dict2) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("\nIs HybridDictionary1 equal to HybridDictionary2? = " + (dict1.Equals(dict2)));
}
}
The output of the above code is −
HybridDictionary1 elements... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear HybridDictionary2 elements... 1 One 2 Two 3 Three 4 Four 5 Five 6 Six Is HybridDictionary1 equal to HybridDictionary2? = False
Custom Content-Based Equality
To compare HybridDictionary objects by content rather than reference, you need to implement custom logic −
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static bool AreEqual(HybridDictionary dict1, HybridDictionary dict2) {
if (dict1.Count != dict2.Count) return false;
foreach (DictionaryEntry entry in dict1) {
if (!dict2.Contains(entry.Key) || !dict2[entry.Key].Equals(entry.Value))
return false;
}
return true;
}
public static void Main() {
HybridDictionary dict1 = new HybridDictionary();
dict1.Add("A", "Books");
dict1.Add("B", "Electronics");
HybridDictionary dict2 = new HybridDictionary();
dict2.Add("A", "Books");
dict2.Add("B", "Electronics");
HybridDictionary dict3 = new HybridDictionary();
dict3.Add("A", "Books");
dict3.Add("C", "Movies");
Console.WriteLine("Reference equality dict1 == dict2: " + dict1.Equals(dict2));
Console.WriteLine("Content equality dict1 == dict2: " + AreEqual(dict1, dict2));
Console.WriteLine("Content equality dict1 == dict3: " + AreEqual(dict1, dict3));
}
}
The output of the above code is −
Reference equality dict1 == dict2: False Content equality dict1 == dict2: True Content equality dict1 == dict3: False
Conclusion
The Equals() method for HybridDictionary performs reference equality comparison by default, returning true only when both variables point to the same object. For content-based equality comparison, you need to implement custom logic that checks if both dictionaries have the same key-value pairs.
