Check if SortedSet and the specified collection contain the same elements in C#

The SetEquals method in C# is used to check if a SortedSet and another collection contain exactly the same elements. This method returns true if both collections have identical elements, regardless of their order, and false otherwise.

Syntax

Following is the syntax for using the SetEquals method −

public bool SetEquals(IEnumerable<T> other)

Parameters

  • other − The collection to compare with the current SortedSet.

Return Value

Returns true if the SortedSet and the specified collection contain the same elements; otherwise, false.

Using SetEquals with Different Elements

When two SortedSet objects contain different elements, SetEquals returns false

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);

      SortedSet<int> set2 = new SortedSet<int>();
      set2.Add(450);
      set2.Add(550);
      set2.Add(650);
      set2.Add(750);
      set2.Add(800);

      Console.WriteLine("Set 1: " + string.Join(", ", set1));
      Console.WriteLine("Set 2: " + string.Join(", ", set2));
      Console.WriteLine("Does it contain the same elements? = " + set1.SetEquals(set2));
   }
}

The output of the above code is −

Set 1: 100, 200, 300
Set 2: 450, 550, 650, 750, 800
Does it contain the same elements? = False

Using SetEquals with Identical Elements

When two collections contain the exact same elements, SetEquals returns true

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);

      SortedSet<int> set2 = new SortedSet<int>();
      set2.Add(300);
      set2.Add(100);
      set2.Add(200);

      Console.WriteLine("Set 1: " + string.Join(", ", set1));
      Console.WriteLine("Set 2: " + string.Join(", ", set2));
      Console.WriteLine("Does it contain the same elements? = " + set1.SetEquals(set2));
   }
}

The output of the above code is −

Set 1: 100, 200, 300
Set 2: 100, 200, 300
Does it contain the same elements? = True

Using SetEquals with Different Collection Types

The SetEquals method can compare a SortedSet with any collection that implements IEnumerable<T>

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      SortedSet<string> sortedSet = new SortedSet<string>();
      sortedSet.Add("Apple");
      sortedSet.Add("Banana");
      sortedSet.Add("Orange");

      List<string> list = new List<string> { "Orange", "Apple", "Banana" };
      string[] array = { "Banana", "Orange", "Apple", "Apple" }; // duplicate element

      Console.WriteLine("SortedSet: " + string.Join(", ", sortedSet));
      Console.WriteLine("List: " + string.Join(", ", list));
      Console.WriteLine("Array: " + string.Join(", ", array));
      Console.WriteLine("SortedSet equals List? " + sortedSet.SetEquals(list));
      Console.WriteLine("SortedSet equals Array? " + sortedSet.SetEquals(array));
   }
}

The output of the above code is −

SortedSet: Apple, Banana, Orange
List: Orange, Apple, Banana
Array: Banana, Orange, Apple, Apple
SortedSet equals List? True
SortedSet equals Array? True

Key Rules

  • SetEquals ignores the order of elements in the collections.

  • Duplicate elements in the other collection are ignored during comparison.

  • Both collections must contain exactly the same unique elements for the method to return true.

  • The method works with any collection type that implements IEnumerable<T>.

Conclusion

The SetEquals method provides an efficient way to compare if two collections contain the same unique elements, regardless of order. It works with various collection types and automatically handles duplicates, making it ideal for set-based comparisons in C#.

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

143 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements