How to get a subset in a SortedSet in C#?

A SortedSet in C# provides the GetViewBetween() method to obtain a subset of elements within a specified range. This method returns a view that contains all elements between the lower and upper bounds, inclusive of both boundaries.

Syntax

Following is the syntax for getting a subset from a SortedSet −

SortedSet<T> subset = sortedSet.GetViewBetween(lowerValue, upperValue);

Parameters

  • lowerValue − The lowest desired value in the view (inclusive).

  • upperValue − The highest desired value in the view (inclusive).

Return Value

The GetViewBetween() method returns a subset view that contains only the elements between the specified bounds, maintaining the sorted order.

Using GetViewBetween() with String Elements

The following example demonstrates how to get a subset from a SortedSet containing string elements −

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        SortedSet<string> set1 = new SortedSet<string>();
        set1.Add("AB");
        set1.Add("BC");
        set1.Add("CD");
        set1.Add("EF");
        Console.WriteLine("Elements in SortedSet1...");
        foreach (string res in set1) {
            Console.WriteLine(res);
        }

        SortedSet<string> set2 = new SortedSet<string>();
        set2.Add("BC");
        set2.Add("CD");
        set2.Add("DE");
        set2.Add("EF");
        set2.Add("AB");
        set2.Add("HI");
        set2.Add("JK");
        Console.WriteLine("Elements in SortedSet2...");
        foreach (string res in set2) {
            Console.WriteLine(res);
        }

        SortedSet<string> set3 = set2.GetViewBetween("CD", "EF");
        Console.WriteLine("Elements in SortedSet3 (subset)...");
        foreach (string res in set3) {
            Console.WriteLine(res);
        }
    }
}

The output of the above code is −

Elements in SortedSet1...
AB
BC
CD
EF
Elements in SortedSet2...
AB
BC
CD
DE
EF
HI
JK
Elements in SortedSet3 (subset)...
CD
DE
EF

Using GetViewBetween() with Integer Elements

The following example shows how to extract a numeric range from a SortedSet −

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        SortedSet<int> set1 = new SortedSet<int>();
        set1.Add(50);
        set1.Add(100);
        set1.Add(150);
        set1.Add(200);
        set1.Add(250);
        set1.Add(300);
        set1.Add(350);
        set1.Add(400);
        set1.Add(450);
        set1.Add(500);
        Console.WriteLine("Elements in SortedSet1...");
        foreach (int res in set1) {
            Console.WriteLine(res);
        }

        SortedSet<int> set2 = set1.GetViewBetween(150, 400);
        Console.WriteLine("Elements in SortedSet2 (subset)...");
        foreach (int res in set2) {
            Console.WriteLine(res);
        }
    }
}

The output of the above code is −

Elements in SortedSet1...
50
100
150
200
250
300
350
400
450
500
Elements in SortedSet2 (subset)...
150
200
250
300
350
400

GetViewBetween() Operation Original SortedSet 50 100 150 200 250 300 400 Subset (150 to 300) GetViewBetween(150, 300) includes both bounds

Key Points

  • The GetViewBetween() method returns a view, not a copy, so changes to the original SortedSet affect the subset.

  • Both lower and upper bounds are inclusive in the returned subset.

  • The subset maintains the sorted order of elements.

  • If no elements exist in the specified range, an empty subset is returned.

Conclusion

The GetViewBetween() method in C# SortedSet provides an efficient way to obtain a subset of elements within a specified range. This method maintains the sorted order and includes both boundary values, making it ideal for range-based queries on sorted collections.

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

201 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements