C# | SortedSet Class - GeeksforGeeks

24 Jan, 2019
SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace.
Characteristics:
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.
Example:
// C# code to create a SortedSetusing System;using System.Collections.Generic;  class GFG {      // Driver code    public static void Main()    {          // Creating a SortedSet of integers        SortedSet<int> mySortedSet = new SortedSet<int>();          // Adding elements in mySortedSet        for (int i = 1; i <= 6; i++) {            mySortedSet.Add(2 * i + 1);        }          // Displaying elements in mySortedSet        Console.WriteLine("The elements in mySortedSet are : ");          // Displaying the element in mySortedSet        foreach(int i in mySortedSet)        {            Console.WriteLine(i);        }    }}
The elements in mySortedSet are : 
3
5
7
9
11
13

Example:
// C# code to illustrate the properties// of SortedSet<T> Classusing System;using System.Collections.Generic;   class GFG {       // Driver code    public static void Main()    {           // Creating a SortedSet of integers        SortedSet<int> mySortedSet = new SortedSet<int>();           // adding elements in mySortedSet        mySortedSet.Add(1);        mySortedSet.Add(2);        mySortedSet.Add(3);        mySortedSet.Add(4);        mySortedSet.Add(5);           // ------ Count property ----------                  // Displaying the number of elements in        // the SortedSet using "Count" property        Console.WriteLine("The number of elements in mySortedSet are: "                                                + mySortedSet.Count);                                                          // ---------- Min property ----------        // Displaying the minimum value in the SortedSet        Console.WriteLine("The minimum element in SortedSet is : "                                                + mySortedSet.Min);    }}
The number of elements in mySortedSet are: 5
The minimum element in SortedSet is : 1

Example:
// C# code to illustrate the methods// of SortedSet<T> Classusing System;using System.Collections.Generic;   class GFG {       // Driver code    public static void Main()    {           // Creating a SortedSet of integers        SortedSet<int> mySortedSet = new SortedSet<int>();           // adding elements in mySortedSet        mySortedSet.Add(2);        mySortedSet.Add(4);        mySortedSet.Add(6);        mySortedSet.Add(8);        mySortedSet.Add(10);           //-------- Remove Method --------                  // Removing element "4" if found        mySortedSet.Remove(4);           // Displaying the elements in mySortedSet        foreach(int i in mySortedSet)        {            Console.WriteLine(i);        }           Console.WriteLine("After Using Method");           // Removing element "14" if found        mySortedSet.Remove(14);           // Displaying the element in mySortedSet        foreach(int i in mySortedSet)        {            Console.WriteLine(i);        }                  // -------- IsSubsetOf Method --------                  // Creating a SortedSet of integers        SortedSet<int> mySet2 = new SortedSet<int>();           // Inserting elements in SortedSet        mySet2.Add(3);        mySet2.Add(4);        mySet2.Add(5);        mySet2.Add(6);           // Check if a SortedSet is a subset        // of the specified collection        // It should return false as SortedSet mySet2        // is not a subset of SortedSet mySet1        Console.WriteLine(mySet2.IsSubsetOf(mySortedSet));    }}
2
6
8
10
After Using Method
2
6
8
10
False

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.sortedset-1?view=netframework-4.7.2
CSharp-Generic-Namespace
CSharp-Generic-SortedSet
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# | Delegates
C# | Abstract Classes
Difference between Ref and Out keywords in C#
C# | Class and Object
C# | Constructors
Extension Method in C#
Introduction to .NET Framework
C# | String.IndexOf( ) Method | Set - 1
C# | Replace() Method
C# | Data Types