C# | Union of SortedSet to a collection Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.UnionWith(IEnumerable<T>) method is used to modify the current SortedSet<T> object so that it contains all elements that are present in either the current object or the specified collection. Properties: In C#, SortedSet class can be used to store, remove or view elements. It maintains ascending order and does not store duplicate elements. It is suggested to use SortedSet class if you have to store unique elements and maintain ascending order. Syntax: mySortedSet1.UnionWith(mySortedSet2); Here, mySortedSet1 and mySortedSet2 are two SortedSet objects. Exception: This method will give ArgumentNullException if the SortedSet is null. Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to get the Union of 2 SortedSets using System; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating a SortedSet of integers SortedSet<int> mySortedSet1 = new SortedSet<int>(); // adding elements in mySortedSet1 mySortedSet1.Add(2); mySortedSet1.Add(4); mySortedSet1.Add(6); mySortedSet1.Add(8); mySortedSet1.Add(10); // Creating a SortedSet of integers SortedSet<int> mySortedSet2 = new SortedSet<int>(); // adding elements in mySortedSet mySortedSet2.Add(4); mySortedSet2.Add(5); mySortedSet2.Add(7); mySortedSet2.Add(8); mySortedSet2.Add(9); Console.WriteLine("The union of mySortedSet1 and mySortedSet2 is: "); mySortedSet1.UnionWith(mySortedSet2); // To display the union of mySortedSet1 and mySortedSet2 foreach(int i in mySortedSet1) { Console.WriteLine(i); } } } Output: The union of mySortedSet1 and mySortedSet2 is: 2 4 5 6 7 8 9 10 Example 2: CSHARP // C# code to get the Union of 2 SortedSets using System; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating a SortedSet of strings SortedSet<string> mySortedSet1 = new SortedSet<string>(); // adding elements in mySortedSet1 mySortedSet1.Add("Geeks"); mySortedSet1.Add("for"); mySortedSet1.Add("Geeks"); mySortedSet1.Add("Noida"); mySortedSet1.Add("Data Structures"); // Creating a SortedSet of strings SortedSet<string> mySortedSet2 = new SortedSet<string>(); // adding elements in mySortedSet mySortedSet2.Add("Geeks"); mySortedSet2.Add("Java"); mySortedSet2.Add("Geeks Classes"); mySortedSet2.Add("C++"); mySortedSet2.Add("Noida"); Console.WriteLine("The union of mySortedSet1 and mySortedSet2 is: "); mySortedSet1.UnionWith(mySortedSet2); // To display the union of mySortedSet1 and mySortedSet2 foreach(string str in mySortedSet1) { Console.WriteLine(str); } } } Output: The union of mySortedSet1 and mySortedSet2 is: C++ Data Structures for Geeks Geeks Classes Java Noida Reference: https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.sortedset-1.unionwith?view=netcore-2.1 Comment More infoAdvertise with us Next Article C# | Check if a SortedSet is a subset of the specified collection S Sahil_Bansall Follow Improve Article Tags : Misc C# CSharp-method CSharp-Generic-SortedSet CSharp-Generic-Namespace +1 More Practice Tags : Misc Similar Reads C# | Intersection of SortedSet with a collection SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.IntersectWith(IEnumerable<T>) method is used to modify the current SortedSet<T> object so that it contains only elements that are also in a spe 2 min read C# | How to create a SortedSet SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. Properties : In C#, SortedSet class can be used to store, remove or view elements. It maintains ascending order and does not store duplicate elements. It is suggeste 2 min read C# | How to create a SortedList SortedList class is a collection of (key, value) pairs which are sorted according to keys. Those pairs can be accessible by key and as well as by index(zero-based indexing). This comes under System.Collections namespace. Properties of SortedList: Internally the object of SortedList maintains two arr 2 min read C# | Add element to SortedSet SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet.Add(T) Method is used to add an element to the set and returns a value that specify if it was successfully added or not. Properties: In C#, SortedSet class 2 min read C# | Check if a SortedSet is a subset of the specified collection SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.IsSubsetOf(IEnumerable<T>) Method is used to check whether a SortedSet<T> object is a subset of the specified collection or not. Properties: In 2 min read C# | How to get a subset in a SortedSet SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.GetViewBetween(T, T) method is used to return a view of a subset in a SortedSet<T>. Properties: In C#, SortedSet class can be used to store, remov 2 min read Like