Add element to SortedSet in C#

The SortedSet<T> class in C# provides a way to store unique elements in sorted order. To add elements to a SortedSet, you use the Add() method, which automatically maintains the sorted order and prevents duplicate entries.

Syntax

Following is the syntax for adding elements to a SortedSet −

SortedSet<T> sortedSet = new SortedSet<T>();
bool added = sortedSet.Add(element);

Parameters

  • element − The element to add to the SortedSet.

Return Value

The Add() method returns a bool value:

  • true − If the element was successfully added (not a duplicate).
  • false − If the element already exists in the SortedSet.

Using Add() with Integer Elements

Example

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        SortedSet<int> set1 = new SortedSet<int>();
        set1.Add(100);
        set1.Add(200);
        set1.Add(300);
        set1.Add(400);
        Console.WriteLine("Elements in SortedSet...");
        foreach (int res in set1) {
            Console.WriteLine(res);
        }
        Console.WriteLine("Does the SortedSet contains the element 500? = " + set1.Contains(500));
    }
}

The output of the above code is −

Elements in SortedSet...
100
200
300
400
Does the SortedSet contains the element 500? = False

Using Add() with String Elements

Example

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        SortedSet<string> set1 = new SortedSet<string>();
        set1.Add("CD");
        set1.Add("CD");  // Duplicate - will not be added
        set1.Add("CD");  // Duplicate - will not be added
        set1.Add("CD");  // Duplicate - will not be added
        Console.WriteLine("Elements in SortedSet1...");
        foreach (string res in set1) {
            Console.WriteLine(res);
        }
        
        SortedSet<string> set2 = new SortedSet<string>();
        set2.Add("BC");
        set2.Add("CD");
        set2.Add("DE");
        set2.Add("EF");
        set2.Add("AB");
        set2.Add("HI");
        set2.Add("JK");
        Console.WriteLine("Elements in SortedSet2...");
        foreach (string res in set2) {
            Console.WriteLine(res);
        }
        Console.WriteLine("SortedSet2 is a superset of SortedSet1? = " + set2.IsSupersetOf(set1));
    }
}

The output of the above code is −

Elements in SortedSet1...
CD
Elements in SortedSet2...
AB
BC
CD
DE
EF
HI
JK
SortedSet2 is a superset of SortedSet1? = True

Checking Add() Return Value

Example

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        SortedSet<int> numbers = new SortedSet<int>();
        
        bool result1 = numbers.Add(50);
        bool result2 = numbers.Add(30);
        bool result3 = numbers.Add(50);  // Duplicate
        
        Console.WriteLine("Adding 50: " + result1);
        Console.WriteLine("Adding 30: " + result2);
        Console.WriteLine("Adding 50 again: " + result3);
        
        Console.WriteLine("Final SortedSet:");
        foreach (int num in numbers) {
            Console.WriteLine(num);
        }
    }
}

The output of the above code is −

Adding 50: True
Adding 30: True
Adding 50 again: False
Final SortedSet:
30
50

Key Features

  • Automatic Sorting − Elements are automatically sorted when added.
  • No Duplicates − Duplicate elements are ignored and not added.
  • Return Value − The Add() method indicates whether the element was successfully added.
  • Generic Collection − Works with any comparable data type.

Conclusion

The Add() method in SortedSet automatically maintains sorted order and prevents duplicates. It returns true for successful additions and false for duplicates, making it easy to track which elements were actually added to the collection.

Updated on: 2026-03-17T07:04:36+05:30

227 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements