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:
// 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);
}
}
} |
C D E F G
Example 2:
// 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);
}
}
} |
3 4 5 6 7
Reference: