Check if two HashSet objects are equal in C#

To check if two HashSet objects are equal in C#, you can use the Equals() method or the SetEquals() method. However, these methods work differently − Equals() checks for reference equality, while SetEquals() checks for content equality regardless of order.

Syntax

Following is the syntax for checking HashSet equality using Equals()

bool result = hashSet1.Equals(hashSet2);

Following is the syntax for checking HashSet content equality using SetEquals()

bool result = hashSet1.SetEquals(hashSet2);

Using Equals() for Reference Equality

The Equals() method checks if two HashSet objects reference the same instance in memory. It returns true only when both variables point to the same HashSet object −

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main(string[] args) {
        HashSet<string> set1 = new HashSet<string>();
        set1.Add("A");
        set1.Add("B");
        set1.Add("C");
        
        HashSet<string> set2 = new HashSet<string>();
        set2.Add("John");
        set2.Add("Jacob");
        set2.Add("Ryan");
        
        HashSet<string> set3 = set2;  // Reference assignment
        
        Console.WriteLine("Is set3 equal to set2 (same reference)? = " + set3.Equals(set2));
        Console.WriteLine("Is set2 equal to set1 (different objects)? = " + set2.Equals(set1));
    }
}

The output of the above code is −

Is set3 equal to set2 (same reference)? = True
Is set2 equal to set1 (different objects)? = False

Using SetEquals() for Content Equality

The SetEquals() method compares the actual contents of two HashSet objects, returning true if they contain the same elements regardless of order −

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main(string[] args) {
        HashSet<string> set1 = new HashSet<string> { "A", "B", "C" };
        HashSet<string> set2 = new HashSet<string> { "C", "A", "B" };
        HashSet<string> set3 = new HashSet<string> { "X", "Y", "Z" };
        
        Console.WriteLine("set1 contents: " + string.Join(", ", set1));
        Console.WriteLine("set2 contents: " + string.Join(", ", set2));
        Console.WriteLine("set3 contents: " + string.Join(", ", set3));
        
        Console.WriteLine("set1.SetEquals(set2): " + set1.SetEquals(set2));
        Console.WriteLine("set1.SetEquals(set3): " + set1.SetEquals(set3));
        Console.WriteLine("set1.Equals(set2): " + set1.Equals(set2));
    }
}

The output of the above code is −

set1 contents: A, B, C
set2 contents: C, A, B
set3 contents: X, Y, Z
set1.SetEquals(set2): True
set1.SetEquals(set3): False
set1.Equals(set2): False

Comparison

Method Comparison Type Use Case
Equals() Reference equality Check if two variables point to the same HashSet instance
SetEquals() Content equality Check if two HashSet objects contain the same elements

Checking Equality with Different Data Types

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main(string[] args) {
        HashSet<int> numbers1 = new HashSet<int> { 1, 2, 3, 4, 5 };
        HashSet<int> numbers2 = new HashSet<int> { 5, 4, 3, 2, 1 };
        HashSet<int> numbers3 = new HashSet<int> { 1, 2, 3 };
        
        Console.WriteLine("numbers1 == numbers2 (same content): " + numbers1.SetEquals(numbers2));
        Console.WriteLine("numbers1 == numbers3 (subset): " + numbers1.SetEquals(numbers3));
        Console.WriteLine("numbers1.Count: " + numbers1.Count);
        Console.WriteLine("numbers3.Count: " + numbers3.Count);
    }
}

The output of the above code is −

numbers1 == numbers2 (same content): True
numbers1 == numbers3 (subset): False
numbers1.Count: 5
numbers3.Count: 3

Conclusion

Use SetEquals() to compare HashSet contents for logical equality, and Equals() to check if two variables reference the same HashSet object. SetEquals() is typically the preferred method for comparing HashSet data since it evaluates actual content rather than memory references.

Updated on: 2026-03-17T07:04:36+05:30

431 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements