Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
C# SortedDictionary Class
Next article icon

C# SortedSet Class

Last Updated : 31 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

SortedSet class in C# represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace.

  • In C#, the 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 the SortedSet class if we have to store unique elements and maintain ascending order.

Example: This example demonstrates how to use a SortedSet, add elements, and display integers in ascending order.

C#
// C# program to add elements in an SortedSet using System; using System.Collections.Generic;  class Geeks {     static void Main()     {         SortedSet<int> s = new SortedSet<int>();          // Adding elements         s.Add(5);         s.Add(1);         s.Add(3);         s.Add(10);          foreach(var i in s) {            Console.WriteLine(i);          }     } } 

Output
1 3 5 10 

Declarartion of SortedSet

In C#, the declaration of SortedSet can be done as:

SortedSet<T> sortedSet = new SortedSet<T>();

Note: T is the type of elements you want to store (e.g. int, String).

Constructors

ConstructorDescription
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: This example demonstrates how to store and display unique odd integers in asceding order.

C#
// C# Program to demonstrates unique odd integers using System; using System.Collections.Generic;  class Geeks {      public static void Main()     {          // Creating a SortedSet of integers          SortedSet<int> s = new SortedSet<int>();          // Adding elements in SortedSet         for (int i = 1; i <= 6; i++) {             s.Add(2 * i + 1);         }           Console.WriteLine("The elements in SortedSet are: ");           foreach(int i in s)         {             Console.WriteLine(i);         }     } }  

Output
The elements in SortedSet are:  3 5 7 9 11 13 

Properties

PropertiesDescription
ComparerGets the IComparer object that is used to order the values in the SortedSet.
CountGets the number of elements in the SortedSet.
MaxGets the maximum value in the SortedSet, as defined by the comparer.
MinGets the minimum value in the SortedSet, as defined by the compare.

Example: This example demonstrates the use of Count and Min properties in SortedSet to get the number of elements and minimum value.

C#
// C# program to demonstrates the SortedSet property using System; using System.Collections.Generic;  class Geeks {      public static void Main()     {         // Creating a SortedSet of integers         SortedSet<int> s = new SortedSet<int>();          // adding elements         s.Add(1);         s.Add(2);         s.Add(3);         s.Add(4);         s.Add(5);          // Displaying the number of elements in         // the SortedSet using "Count" property         Console.WriteLine(             "The number of elements in SorteSet are: "             + s.Count);          // Displaying the minimum value in the SortedSet         // using "Min" property         Console.WriteLine(             "The minimum element in SortedSet is: "             + s.Min);     } } 

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

Methods

MethodsDescription
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: This example demonstrates how to remove an element and display the remaining elements in sorted order.

C#
// C# program to demonstrates the working of Remove() using System; using System.Collections.Generic;  class Geeks {          public static void Main()     {          // Creating a SortedSet of integers          SortedSet<int> s = new SortedSet<int>();          // adding elements          s.Add(2);         s.Add(4);         s.Add(6);         s.Add(8);         s.Add(10);          // Removing element "4" if found          s.Remove(4);          // Displaying the elements          foreach(int i in s)         {             Console.WriteLine(i);         }     } } 

Output
2 6 8 10 

Example: This example demonstrates how to check if s2 is a subset of s1 using IsSubsetOf() method.

C#
// C# program to demonstrates the working of IsSubseOf() using System; using System.Collections.Generic;  class Geeks {     public static void Main()     {         // Creating two SortedSets of integers         SortedSet<int> s1 = new SortedSet<int>();         s1.Add(2);         s1.Add(4);         s1.Add(6);         s1.Add(8);         s1.Add(10);          SortedSet<int> s2 = new SortedSet<int>();         s2.Add(2);         s2.Add(4);         s2.Add(6);          Console.WriteLine(s2.IsSubsetOf(s1));     } } 

Output
True 

Next Article
C# SortedDictionary Class

S

Sahil_Bansall
Improve
Article Tags :
  • C#
  • CSharp-Generic-SortedSet
  • CSharp-Generic-Namespace

Similar Reads

  • C# SortedList Class
    SortedList class in C# is a collection of key-value pairs that are sorted by keys. By default, it sorts the key-value pairs in ascending order. It is both a generic and non-generic type, of collection. The generic SortedList is defined in the System.Collections.Generic namespace whereas non-generic
    6 min read
  • C# SortedSet
    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.
    5 min read
  • C# HashSet Class
    In C#, a HashSet<T> class is an unordered collection of unique elements. It comes under System.Collections.Generic namespace. It is used in a situation where we want to prevent duplicates from being inserted in the collection. In terms of performance, it is generally better than a list. It doe
    6 min read
  • C# SortedDictionary Class
    In C#, the SortedDictionary<TKey,TValue> class represents the collection of key/value pairs. This pair is in sorted form and the sorting is done on the key. This class is defined under System.Collections.Generic namespace. In the SortedDictionary class, the keys are immutable, always unique, a
    5 min read
  • C# SortedList
    In C#, SortedList is a collection of key-value pairs sorted according to keys. By default, this collection sorts ascendingly It is of both generic and non-generic type of collection. The generic SortedList is defined in the System.Collections.Generic namespace whereas non-generic SortedList is defin
    7 min read
  • C# | How to create a SortedSet
    SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. 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 suggeste
    2 min read
  • C# | Add element to SortedSet
    SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet.Add(T) Method is used to add an element to the set and returns a value that specify if it was successfully added or not. Properties: In C#, SortedSet class
    2 min read
  • C# | How to create a SortedList
    SortedList class is a collection of (key, value) pairs which are sorted according to keys. Those pairs can be accessible by key and as well as by index(zero-based indexing). This comes under System.Collections namespace. Properties of SortedList: Internally the object of SortedList maintains two arr
    2 min read
  • C# | Search in a SortedList object
    SortedList class is a collection of (key, value) pairs which are sorted according to keys. Those pairs can be accessible by key and as well as by index(zero-based indexing). This comes under System.Collections namespace. SortedList.ContainsKey(Object) method is used to check whether a SortedList obj
    3 min read
  • C# | Union of SortedSet to a collection
    SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.UnionWith(IEnumerable<T>) method is used to modify the current SortedSet<T> object so that it contains all elements that are present in either
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences