C# | Add element to SortedSet Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 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. Syntax: public bool Add (T item); Parameter: item: The element which is added to the set. Return Value: True if item is added to the set, otherwise False. Example 1: CSHARP // C# code to add element to 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 = 2; i < 7; i++) { mySortedSet.Add(i * 2); } // Displaying elements in mySortedSet foreach(int i in mySortedSet) { Console.WriteLine(i); } } } Output: 4 6 8 10 12 Example 2: CSHARP // C# code to add element to 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 mySortedSet.Add(4); mySortedSet.Add(5); mySortedSet.Add(6); // Trying to add some duplicate // elements in mySortedSet mySortedSet.Add(6); mySortedSet.Add(6); mySortedSet.Add(6); mySortedSet.Add(7); // Displaying elements in mySortedSet foreach(int i in mySortedSet) { Console.WriteLine(i); } } } Output: 4 5 6 7 Reference: https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.sortedset-1.add?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Remove all elements from the SortedSet S Sahil_Bansall Follow Improve Article Tags : Misc C# CSharp-method CSharp-Generic-SortedSet CSharp-Generic-Namespace +1 More Practice Tags : Misc Similar Reads 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# | Remove all elements from the SortedSet SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.Clear Method is used to remove the all elements from the SortedSet. Properties: In C#, SortedSet class can be used to store, remove or view elements. 2 min read C# | Remove all elements from the SortedSet SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.Clear Method is used to remove the all elements from the SortedSet. Properties: In C#, SortedSet class can be used to store, remove or view elements. 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# | 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# | 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 Like