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 SortedSet objects are equal in C#
In C#, checking if two SortedSet objects are equal involves understanding that SortedSet<T> uses reference equality by default with the Equals() method. This means two sets are considered equal only if they reference the same object in memory, not if they contain the same elements.
To properly check if two SortedSet objects contain the same elements, you need to use the SetEquals() method or implement custom comparison logic.
Using Equals() Method (Reference Equality)
The Equals() method checks if two SortedSet objects reference the same instance in memory −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
SortedSet<int> set1 = new SortedSet<int>();
set1.Add(100);
set1.Add(200);
set1.Add(300);
set1.Add(400);
Console.WriteLine("Elements in SortedSet1...");
foreach (int res in set1) {
Console.WriteLine(res);
}
SortedSet<int> set2 = new SortedSet<int>();
set2.Add(100);
set2.Add(200);
set2.Add(300);
set2.Add(400);
Console.WriteLine("Elements in SortedSet2...");
foreach (int res in set2) {
Console.WriteLine(res);
}
Console.WriteLine("Are both sets equal (reference)? = " + set1.Equals(set2));
// Assign set1 to set2 (same reference)
set2 = set1;
Console.WriteLine("After assignment, are they equal? = " + set1.Equals(set2));
}
}
The output of the above code is −
Elements in SortedSet1... 100 200 300 400 Elements in SortedSet2... 100 200 300 400 Are both sets equal (reference)? = False After assignment, are they equal? = True
Using SetEquals() Method (Content Equality)
To check if two SortedSet objects contain the same elements, use the SetEquals() method −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
SortedSet<int> set1 = new SortedSet<int>();
set1.Add(10);
set1.Add(20);
set1.Add(30);
SortedSet<int> set2 = new SortedSet<int>();
set2.Add(30);
set2.Add(10);
set2.Add(20);
SortedSet<int> set3 = new SortedSet<int>();
set3.Add(10);
set3.Add(20);
set3.Add(40);
Console.WriteLine("Set1 contains: " + string.Join(", ", set1));
Console.WriteLine("Set2 contains: " + string.Join(", ", set2));
Console.WriteLine("Set3 contains: " + 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 contains: 10, 20, 30 Set2 contains: 10, 20, 30 Set3 contains: 10, 20, 40 set1.SetEquals(set2): True set1.SetEquals(set3): False set1.Equals(set2): False
Comparison of Equality Methods
| Method | Type of Equality | Use Case |
|---|---|---|
Equals() |
Reference equality | Check if two variables point to the same object |
SetEquals() |
Content equality | Check if two sets contain the same elements |
Using LINQ for Content Equality
Another approach to check content equality is using LINQ's SequenceEqual() method −
using System;
using System.Collections.Generic;
using System.Linq;
public class Demo {
public static void Main() {
SortedSet<string> set1 = new SortedSet<string> {"apple", "banana", "cherry"};
SortedSet<string> set2 = new SortedSet<string> {"cherry", "apple", "banana"};
SortedSet<string> set3 = new SortedSet<string> {"apple", "banana"};
Console.WriteLine("Set1: " + string.Join(", ", set1));
Console.WriteLine("Set2: " + string.Join(", ", set2));
Console.WriteLine("Set3: " + string.Join(", ", set3));
Console.WriteLine("Using SetEquals():");
Console.WriteLine("set1.SetEquals(set2): " + set1.SetEquals(set2));
Console.WriteLine("set1.SetEquals(set3): " + set1.SetEquals(set3));
Console.WriteLine("Using SequenceEqual():");
Console.WriteLine("set1.SequenceEqual(set2): " + set1.SequenceEqual(set2));
Console.WriteLine("set1.SequenceEqual(set3): " + set1.SequenceEqual(set3));
}
}
The output of the above code is −
Set1: apple, banana, cherry Set2: apple, banana, cherry Set3: apple, banana Using SetEquals(): set1.SetEquals(set2): True set1.SetEquals(set3): False Using SequenceEqual(): set1.SequenceEqual(set2): True set1.SequenceEqual(set3): False
Conclusion
To check if two SortedSet objects are equal in C#, use SetEquals() for content equality (same elements) or Equals() for reference equality (same object instance). The SetEquals() method is generally more useful as it compares the actual elements rather than object references.
