A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/c-sharp/sortedset-in-c-sharp-with-examples/ below:

C# SortedSet - GeeksforGeeks

C# SortedSet

Last Updated : 11 Jul, 2025

SortedSet in C# is a collection of objects that stores elements uniquely in sorted order. It is of the generic type collection and is defined under System.Collections.Generic namespace. It is a dynamic collection means the size of the SortedSet is automatically increased when new elements are added. There are some key features mentioned below:

Example: Creating and Displaying a SortedSet

C#
// C# program to demonstrate the use of SortedSet
using System;
using System.Collections.Generic;

class Geeks {
    public static void Main() {
      
        // Creating a SortedSet
        SortedSet<int> num = 
          new SortedSet<int> { 7, 1, 2, 8, 1, 4 };

        // Adding elements
        num.Add(6);
      
        // Adding duplicate (will not be added)
        num.Add(2);

        // Displaying elements
        Console.WriteLine("SortedSet elements:");
        foreach (int ele in num)
            Console.Write(ele + " ");
    }
}

Output
SortedSet elements:
1 2 4 6 7 8 
Steps to Create SortedSet

SortedSet Class provides 7 different types of constructors for creating a SortedSet, here we use SortedSet(), constructor, which is used to create an instance of the SortedSet class.

Step 1: Include System.Collections.Generic namespace

using System.Collections.Generic;

Step 2: Create a SortedSet using the SortedSet Class

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

Performing Different Operations on SortedSet 1. Adding Element

// Using the Add() method
SortedSet<int> num= new SortedSet<int>();
num.Add(1);
numAdd(2);

// Using Collection Initalizer
SortedSet<int> num = new SortedSet<int> { 2, 1, 4,3 };

2. Accessing Elements

Using foreach loop: We can use for loop to Iterate each element to access the the sorted set

SortedSet<int> num = new SortedSet<int> { 2,3,4,5};

foreach (int ele in num)
{
Console.WriteLine(ele);
}

Using ForEach loop with Lambda Expressions: We can use the ForEach loop with lambda expressions to access the elements of sorted set which reduces the number of code lines and enhances the code readability

SortedSet<int> num = new SortedSet<int> { 2,3,4,5};
numbers.ToList().ForEach(num => Console.WriteLine(num));

Using ElementAt() with LINQ: The SortedSet does not directly support the Indexes but we can use LINQ and use ElementAt() method.

SortedSet<int> num = new SortedSet<int> { 2,3,4,5 };
Console.WriteLine(num.ElementAt(0));

Example: Accessing Elements Using Different Methods

C#
// C# program to demonstrate various 
// ways to access elements in a SortedSet
using System;
using System.Linq;
using System.Collections.Generic;

class Geeks {
    static void Main() {
      
        // Creating a SortedSet using Collection Initializer
        SortedSet<int> num = new SortedSet<int> { 2, 3, 4, 5 };

        // Accessing elements using foreach loop
        Console.WriteLine("Accessing using foreach loop:");
        foreach (int ele in num)
            Console.Write(ele + " ");
        Console.WriteLine();

        // Creating a SortedSet using Add() method
        SortedSet<int> num2 = new SortedSet<int>();
        num2.Add(1);
        num2.Add(2);
        num2.Add(3);

        // Accessing elements using ForEach loop
        Console.WriteLine("Accessing using ForEach loop:");
        num2.ToList().ForEach(ele => Console.Write(ele + " "));
        Console.WriteLine();
    }
}

Output
Accessing using foreach loop:
2 3 4 5 
Accessing using ForEach loop:
1 2 3 
3. Removing Elements

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

Example:

C#
// Removing elements from SortedSet
using System;
using System.Collections.Generic;

class Geeks
{
    static public void Main()
    {
        // Creating SortedSet
        // Using SortedSet class
        SortedSet<int> set = new SortedSet<int>();

        // Add the elements in SortedSet
        // Using Add method
        set.Add(1);
        set.Add(2);
        set.Add(3);
        set.Add(4);

        // After using Remove method
        Console.WriteLine("Total number of elements " +
            "present in set:{0}", set.Count);

        // Remove element from SortedSet
        // Using Remove method
        set.Remove(1);

        // Before using Remove method
        Console.WriteLine("Total number of elements " +
            "present in set:{0}", set.Count);

        // Remove all elements from SortedSet
        // Using Clear method
        set.Clear();
        Console.WriteLine("Total number of elements " +
            "present in set:{0}", set.Count);
    }
}

Output
Total number of elements present in set:4
Total number of elements present in set:3
Total number of elements present in set:0
4. Check If an Element Exists

In SortedSet, we can check the presence of an element using the Contains method. This method is used to determine whether the set contains a specific element.

Example:

C#
// C# program to illustrate how to check
// availability of elements in SortedSet
using System;
using System.Collections.Generic;

public class Geeks{
	static public void Main()
	{
		// Creating SortedSet
		// Using SortedSet class
		SortedSet<int> set = new SortedSet<int>();

		// Add the elements in SortedSet
		// Using Add method
        set.Add(1);
        set.Add(2);
        set.Add(3);
        set.Add(4);
      
		// Check the availability of element
		// Using Contains method
		if (set.Contains(1)) 
			Console.WriteLine("Element is available.");

		else
			Console.WriteLine("Element is not available.");
	}
}

Output
Element is available.
Important Points

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4