A RetroSearch Logo

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

Search Query:

Showing content from https://www.geeksforgeeks.org/c-sharp/c-sharp-add-element-to-sortedset/ below:

C# | Add element to SortedSet

C# | Add element to SortedSet

Last Updated : 11 Jul, 2025

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: 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);
        }
    }
}
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);
        }
    }
}
Reference:

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