In C#, HashSet.SetEquals(other) asks: do the set and other hold exactly the same items? It returns true or false. The order does not matter and repeats do not matter. Only the question "which distinct items are present?" counts.
That makes it the right tool for comparing sets. Do not use == or Equals on two HashSet variables for this, because those ask whether both variables point to the same object, not whether the contents match.
This is an instance member of HashSet<T> (System.Collections.Generic). Call it on a set variable, such as numbers.SetEquals(...). A HashSet<T> is a class, so two variables can share one set. It keeps unique items only and does not promise any order.
Syntax
Call SetEquals on one set and pass the other collection.
C#
HashSet<int> a = new HashSet<int> { 1, 2, 3 };
bool x = a.SetEquals(new int[] { 3, 2, 1 }); // true: same items, any order
bool y = a.SetEquals(new int[] { 1, 1, 2, 2, 3 }); // true: repeats are ignored
bool z = a.SetEquals(new int[] { 1, 2 }); // false: 3 is missing
Call
What it does
a.SetEquals(b)
true when a and b have the same distinct items.
a.SetEquals(new int[] { 1, 1, 2 })
Repeats in the argument are ignored, so only the distinct items are compared.
a == b
Not a content check. It is true only when both names refer to one object.
Passing null throws ArgumentNullException. Two empty collections are equal. Both collections are only read. If you need to know whether the counts of repeated items also match, a set is the wrong tool; use a list or a dictionary of counts.
Library declarations
C#
public bool SetEquals(IEnumerable<T> other);
Parameters
Parameter
Description
other
Required. The collection to compare with. It can be a set, array, or list, and may contain repeats. It must not be null.
Return Value
Return Value
Description
bool
true if the set and other contain exactly the same distinct items. Otherwise false.
C# Examples
Example 1 — Compare Three Collections
Compare {1, 2, 3} with the same numbers in a different order, with a smaller list, and with a bigger list.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
HashSet<int> a = new HashSet<int> { 1, 2, 3 };
Console.WriteLine(a.SetEquals(new int[] { 3, 2, 1 }));
Console.WriteLine(a.SetEquals(new int[] { 1, 2 }));
Console.WriteLine(a.SetEquals(new int[] { 1, 2, 3, 4 }));
Console.WriteLine(a.Count);
}
}
Output
True
False
False
3
How It Works
Same items. The array holds 3, 2, 1. The order is different, but a set has no order, so SetEquals is True.
Too few. The array lacks the 3, so the contents differ and the answer is False.
Too many. The array has an extra 4. The sets must match both ways, so this is False as well.
Read-only. Comparing changes nothing, so a.Count is still 3.
Example 2 — Repeats, Lists, and Empty Sets
Show that repeats are ignored, that a List works as the argument, that two empty sets are equal, and that == is not a content check.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
HashSet<int> a = new HashSet<int> { 1, 2, 3 };
Console.WriteLine(a.SetEquals(new int[] { 1, 1, 2, 2, 3 }));
Console.WriteLine(a.SetEquals(new List<int> { 3, 1, 2 }));
HashSet<int> e1 = new HashSet<int>();
HashSet<int> e2 = new HashSet<int>();
Console.WriteLine(e1.SetEquals(e2));
Console.WriteLine(e1 == e2);
Console.WriteLine(e1.SetEquals(new int[] { 1 }));
}
}
Output
True
True
True
False
False
How It Works
Repeats ignored. The array lists 1 and 2 twice, but the distinct items are still 1, 2, and 3, so the first answer is True.
Any collection. A List<int> works as the argument just like an array. Order is irrelevant.
Empty equals empty. Two empty sets hold the same items (none), so SetEquals is True.
== is a trap.e1 == e2 checks whether both names point to one object, and they do not, so it is False. The last line is False because the empty set does not hold the 1.
Example 3 — Do Two Words Use the Same Letters?
Turn each word into a set of its letters and compare the sets. A string is a collection of characters, so it fits the argument directly.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
HashSet<char> listen = new HashSet<char>("listen");
Console.WriteLine(listen.SetEquals("silent"));
Console.WriteLine(listen.SetEquals("hello"));
Console.WriteLine(listen.Count);
HashSet<char> noon = new HashSet<char>("noon");
Console.WriteLine(noon.SetEquals("no"));
}
}
Output
True
False
6
True
How It Works
String as a collection. A string is an IEnumerable<char>, so new HashSet<char>("listen") stores its six different letters.
Same letters."silent" uses exactly l, i, s, t, e, n in a different order, so SetEquals prints True.
Different letters."hello" has h and o, which are not in the set, and lacks several of its letters, so the answer is False.
Counts are ignored."noon" and "no" both use only n and o, so the sets are equal even though one word is longer. For a real anagram check you would also compare how many times each letter appears.
Frequently Asked Questions
It returns true if the set and the other collection contain exactly the same distinct items, no matter the order and no matter how often an item repeats in the other collection.
The == operator checks whether both variables refer to the same object. Two different sets with the same items are not == equal, but SetEquals is true for them.
No. A set has no order, and repeats in the other collection are ignored. Only the distinct items matter.
Yes. SetEquals accepts any IEnumerable<T>, including a List or an array.
Yes. They contain exactly the same items, which is none, so SetEquals returns true.