C# | How to get a subset in a SortedSet 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>.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, 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: public virtual System.Collections.Generic.SortedSet<T> GetViewBetween (T lowerValue, T upperValue); Parameters: lowerValue: The lowest desired value in the view. upperValue: The highest desired value in the view. Return Value: A subset view that contains only the values in the specified range. Exceptions : ArgumentException: If lowerValue is more than upperValue according to the comparer. ArgumentOutOfRangeException: A tried operation on the view was outside the range specified by lowerValue and upperValue. Example 1: CSHARP // C# code to get the subset of a SortedSet using System; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating a SortedSet of strings SortedSet<string> mySet1 = new SortedSet<string>(); // Inserting elements in SortedSet mySet1.Add("A"); mySet1.Add("B"); mySet1.Add("C"); mySet1.Add("D"); mySet1.Add("E"); mySet1.Add("F"); mySet1.Add("G"); mySet1.Add("H"); mySet1.Add("I"); // Get the subset between "C" and "G" SortedSet<string> mySet2 = mySet1.GetViewBetween("C", "G"); // Displaying the elements in the subset foreach(string str in mySet2) { Console.WriteLine(str); } } } Output: C D E F G Example 2: CSHARP // C# code to get the subset of a SortedSet using System; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating a SortedSet of integers SortedSet<int> mySet1 = new SortedSet<int>(); // Inserting elements in SortedSet for (int i = 0; i < 10; i++) { mySet1.Add(i); } // Get the subset between "3" and "7" SortedSet<int> mySet2 = mySet1.GetViewBetween(3, 7); // Displaying the elements in the subset foreach(int i in mySet2) { Console.WriteLine(i); } } } Output: 3 4 5 6 7 Reference: https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.sortedset-1.getviewbetween?view=netcore-2.1 Comment More infoAdvertise with us Next Article SortedSet tailSet() method in Java 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# | 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 SortedSet subSet() method in Java The subSet() method of SortedSet interface in Java is used to return a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive. The set returned by this method is backed by this set, so changes in the returned set are reflected in this set, and vice- 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 SortedSet tailSet() method in Java The tailSet() method of SortedSet interface in Java is used to return a view of the portion of this set whose elements are greater than or equal to the parameter fromElement. The set returned by this method is backed by this set, so changes in the returned set are reflected in this set, and vice-ver 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# | 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 Like