C# | SortedSet Class

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.

Constructors

Constructor Description
SortedSet() Initializes a new instance of the SortedSet class.
SortedSet(IComparer) Initializes a new instance of the SortedSet class that uses a specified comparer.
SortedSet(IEnumerable) Initializes a new instance of the SortedSet class that contains elements copied from a specified enumerable collection.
SortedSet(IEnumerable, IComparer) Initializes a new instance of the SortedSet class that contains elements copied from a specified enumerable collection and that uses a specified comparer.
SortedSet(SerializationInfo, StreamingContext) Initializes a new instance of the SortedSet class that contains serialized data.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to create a SortedSet
using 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);
        }
    }
}

chevron_right


Output:

The elements in mySortedSet are : 
3
5
7
9
11
13

Properties

Property Description
Comparer Gets the IComparer object that is used to order the values in the SortedSet.
Count Gets the number of elements in the SortedSet.
Max Gets the maximum value in the SortedSet, as defined by the comparer.
Min Gets the minimum value in the SortedSet, as defined by the comparer.

Example:



filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to illustrate the properties
// of SortedSet<T> Class
using 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);
    }
}

chevron_right


Output:

The number of elements in mySortedSet are: 5
The minimum element in SortedSet is : 1

Methods

Method Description
Add(T) Adds an element to the set and returns a value that indicates if it was successfully added.
Clear() Removes all elements from the set.
Contains(T) Determines whether the set contains a specific element.
CopyTo() Copies a portion or all of a SortedSet<T> to a compatible one-dimensional array, starting at the beginning of the destination array or at a specified index.
CreateSetComparer() Returns an IEqualityComparer object that can be used to create a collection that contains individual sets.
CreateSetComparer(IEqualityComparer) Returns an IEqualityComparer object, according to a specified comparer, that can be used to create a collection that contains individual sets.
Equals(Object) Determines whether the specified object is equal to the current object.
ExceptWith(IEnumerable) Removes all elements that are in a specified collection from the current SortedSet object.
GetEnumerator() Returns an enumerator that iterates through the SortedSet.
GetHashCode() Serves as the default hash function.
GetObjectData(SerializationInfo, StreamingContext) Implements the ISerializable interface and returns the data that you must have to serialize a SortedSet object.
GetType() Gets the Type of the current instance.
GetViewBetween(T, T) Returns a view of a subset in a SortedSet.
IntersectWith(IEnumerable) Modifies the current SortedSet object so that it contains only elements that are also in a specified collection.
IsProperSubsetOf(IEnumerable) Determines whether a SortedSet object is a proper subset of the specified collection.
IsProperSupersetOf(IEnumerable) Determines whether a SortedSet object is a proper superset of the specified collection.
IsSubsetOf(IEnumerable) Determines whether a SortedSet object is a subset of the specified collection.
IsSupersetOf(IEnumerable) Determines whether a SortedSet object is a superset of the specified collection.
MemberwiseClone() Creates a shallow copy of the current Object.
OnDeserialization(Object) Implements the ISerializable interface, and raises the deserialization event when the deserialization is completed.
Overlaps(IEnumerable) Determines whether the current SortedSet object and a specified collection share common elements.
Remove(T) Removes a specified item from the SortedSet.
RemoveWhere(Predicate) Removes all elements that match the conditions defined by the specified predicate from a SortedSet.
Reverse() Returns an IEnumerable that iterates over the SortedSet in reverse order.
SetEquals(IEnumerable) Determines whether the current SortedSet object and the specified collection contain the same elements.
SymmetricExceptWith(IEnumerable) Modifies the current SortedSet object so that it contains only elements that are present either in the current object or in the specified collection, but not both.
ToString() Returns a string that represents the current object.
TryGetValue(T, T) Searches the set for a given value and returns the equal value it finds, if any.
UnionWith(IEnumerable) Modifies the current SortedSet object so that it contains all elements that are present in either the current object or the specified collection.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to illustrate the methods
// of SortedSet<T> Class
using 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));
    }
}

chevron_right


Output:

2
6
8
10
After Using Method
2
6
8
10
False

Reference:



My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.