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 a Hashtable is equal to another Hashtable in C#
Checking if a Hashtable is equal to another Hashtable in C# can be done using the Equals() method. However, it's important to understand that this method performs reference equality checking by default, not content comparison.
Syntax
Following is the syntax to check Hashtable equality −
bool result = hashtable1.Equals(hashtable2);
Using Equals() Method for Reference Comparison
The Equals() method checks if two Hashtables are the same reference, not if they contain the same key-value pairs −
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("Hashtable1...");
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("\nHashtable2...");
foreach (string k in key) {
Console.WriteLine(k + ": " + hash2[k]);
}
Console.WriteLine("\nAre both the Hashtable equal? " + (hash1.Equals(hash2)));
}
}
The output of the above code is −
Hashtable1... 8: Stephen 7: Tom 6: Steve 5: Kevin 4: Gary 3: Tim 2: Steve 1: Kevin Hashtable2... 4: Tim 3: John 2: Steve 1: Kevin Are both the Hashtable equal? False
Reference Equality vs Content Equality
Let's demonstrate the difference between reference equality and content equality −
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("Hashtable1...");
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("\nHashtable2...");
foreach (string k in key2) {
Console.WriteLine(k + ": " + hash2[k]);
}
// Reference equality check
Console.WriteLine("\nReference equality: " + (hash1.Equals(hash2)));
// Same reference check
Hashtable hash3 = hash1;
Console.WriteLine("Same reference equality: " + (hash1.Equals(hash3)));
}
}
The output of the above code is −
Hashtable1... 4: Tim 3: John 2: Steve 1: Kevin Hashtable2... 5: Steve 4: Tim 3: John 2: Gary 1: Nathan Reference equality: False Same reference equality: True
Custom Content Equality Method
To perform content-based equality checking, you need to create a custom method that compares keys and values −
using System;
using System.Collections;
public class Demo {
public static bool AreHashtablesEqual(Hashtable h1, Hashtable h2) {
if (h1.Count != h2.Count) return false;
foreach (DictionaryEntry entry in h1) {
if (!h2.ContainsKey(entry.Key) || !h2[entry.Key].Equals(entry.Value)) {
return false;
}
}
return true;
}
public static void Main() {
Hashtable hash1 = new Hashtable();
hash1.Add("1", "Kevin");
hash1.Add("2", "Steve");
Hashtable hash2 = new Hashtable();
hash2.Add("1", "Kevin");
hash2.Add("2", "Steve");
Hashtable hash3 = new Hashtable();
hash3.Add("1", "Kevin");
hash3.Add("2", "John");
Console.WriteLine("Reference equality (hash1 vs hash2): " + hash1.Equals(hash2));
Console.WriteLine("Content equality (hash1 vs hash2): " + AreHashtablesEqual(hash1, hash2));
Console.WriteLine("Content equality (hash1 vs hash3): " + AreHashtablesEqual(hash1, hash3));
}
}
The output of the above code is −
Reference equality (hash1 vs hash2): False Content equality (hash1 vs hash2): True Content equality (hash1 vs hash3): False
Conclusion
The Equals() method on Hashtables in C# performs reference equality checking, not content comparison. For content-based equality, you need to implement a custom method that compares the keys and values of both Hashtables.
