SortedSet in C# with Examples

In C#, SortedSet is a collection of objects in sorted order. It is of the generic type collection and defined under System.Collections.Generic namespace. It will also provides many mathematical set operations, such as intersection, union, and difference. It’s a dynamic collection means the size of the SortedSet is automatically increased when the new elements are added.

Important Points:

  • The SortedSet class implements the ICollectionIEnumerableIReadOnlyCollectionISetICollectionIEnumerableIDeserializationCallback, and ISerializable interfaces.
  • The capacity of a SortedSet is the number of elements it can hold.
  • In SortedSet, the elements must be unique.
  • In SortedSet, the order of the element is ascending.
  • It will be generally used when we want to use SortedSet class if you have to store unique elements and maintain ascending order.
  • In SortedSet, the user can only store the same type of elements.

How to create a SortedSet?

The SortedSet class provides 5 different types of constructors which are used to create a SortedSet, here we only use SortedSet(), constructor.

SortedSet(): It is used to create an instance of the SortedSet class.

Step 1: Include System.Collections.Generic namespace in your program with the help of using keyword:

using System.Collections.Generic;

Step 2: Create a SortedSet using the SortedSet class as shown below:

SortedSet<type_of_sortedset> sortedset_name = new SortedSet<type_of_sortedset>();

Step 3: If you want to add elements in your SortedSet, then use Add() method to add elements in the SortedSet. And you can also store elements in your SortedSet using collection initializer.

Step 4: The elements of SortedSet is accessed by using a foreach loop. As shown in the below example.

Example:

// C# program to illustrate how to
// create SortedSet
using System;
using System.Collections.Generic;
 
class TAS {
 
    // Driver Class
    static public void Main()
    {
 
        // Creating SortedSet
        // Using SortedSet class
        SortedSet<int> my_Set1 = new SortedSet<int>();
 
        // Add the elements in SortedSet
        // Using Add method
        my_Set1.Add(101);
        my_Set1.Add(1001);
        my_Set1.Add(10001);
        my_Set1.Add(100001);
        Console.WriteLine("Elements of my_Set1:");
 
        // Accessing elements of SortedSet
        // Using foreach loop
        foreach(var val in my_Set1)
        {
            Console.WriteLine(val);
        }
 
        // Creating another SortedSet
        // using collection initializer
        // to initialize SortedSet
        SortedSet<int> my_Set2 = new SortedSet<int>() {
                                202,2002,20002,200002};
                 
        // Display elements of my_Set2
        Console.WriteLine("Elements of my_Set2:");
        foreach(var value in my_Set2)
        {
            Console.WriteLine(value);
        }
    }
}

Output:

Elements of my_Set1:
101
1001
10001
100001
Elements of my_Set2:
202
2002
20002
200002

How to remove elements from the SortedSet?

In SortedSet, you are allowed to remove elements from the SortedSet. SortedSet<T> class provides three different methods to remove elements and the methods are:

  • Remove(T): This method is used to remove a specified item from the SortedSet.
  • RemoveWhere(Predicate): This method is used to remove all elements that match the conditions defined by the specified predicate from a SortedSet.
  • Clear(): This method is used to remove all elements from the set.

Example:

// C# program to illustrate how to
// remove elements from SortedSet
using System;
using System.Collections.Generic;
 
class TAS {
 
    // Main Method
    static public void Main()
    {
 
        // Creating SortedSet
        // Using SortedSet class
        SortedSet<int> my_Set = new SortedSet<int>();
 
        // Add the elements in SortedSet
        // Using Add method
        my_Set.Add(101);
        my_Set.Add(1001);
        my_Set.Add(10001);
        my_Set.Add(100001);
 
        // After using Remove method
        Console.WriteLine("Total number of elements "+
               "present in my_Set:{0}", my_Set.Count);
 
        // Remove element from SortedSet
        // Using Remove method
        my_Set.Remove(1001);
 
        // Before using Remove method
        Console.WriteLine("Total number of elements "+
               "present in my_Set:{0}", my_Set.Count);
 
        // Remove all elements from SortedSet
        // Using Clear method
        my_Set.Clear();
        Console.WriteLine("Total number of elements "+
               "present in my_Set:{0}", my_Set.Count);
    }
}

Output:

Total number of elements present in my_Set:4
Total number of elements present in my_Set:3
Total number of elements present in my_Set:0

How to check the availability of elements in SortedSet?

In SortedSet, you can check whether the given element is present or not using the Contains method. It is used to determine whether the set contains a specific element.

Example:

// C# program to illustrate how to check
// availability of elements in SortedSet
using System;
using System.Collections.Generic;
 
public class TAS {
    static public void Main()
    {
 
        // Creating SortedSet
        // Using SortedSet class
        SortedSet<int> my_Set = new SortedSet<int>();
 
        // Add the elements in SortedSet
        // Using Add method
        my_Set.Add(101);
        my_Set.Add(1001);
        my_Set.Add(10001);
        my_Set.Add(100001);
 
        // Check the given element present
        // in the SortedSet or not
        // Using Contains method
        if (my_Set.Contains(101) == true)
        {
            Console.WriteLine("Element is available..!");
        }
 
        else
        {
            Console.WriteLine("Element is not available..!");
        }
    }
}

Output:

Element is available..!

Chockalingam