SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.Count Property is used to get the number of elements in the SortedSet.
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:
mySortedSet.Count
Here, mySortedSet is a SortedSet.
Below given are some examples to understand the implementation in a better way:
Example 1:
using System;
using System.Collections.Generic;
class GFG {
public static void Main()
{
SortedSet< int > mySortedSet = new SortedSet< int >();
mySortedSet.Add(1);
mySortedSet.Add(2);
mySortedSet.Add(3);
mySortedSet.Add(4);
mySortedSet.Add(5);
Console.WriteLine( "The number of elements in mySortedSet are: "
+ mySortedSet.Count);
}
}
|
Output:
The number of elements in mySortedSet are: 5
Example 2:
using System;
using System.Collections.Generic;
class GFG {
public static void Main()
{
SortedSet< string > mySortedSet = new SortedSet< string >();
mySortedSet.Add( "Hey" );
mySortedSet.Add( "GeeksforGeeks" );
mySortedSet.Add( "and" );
mySortedSet.Add( "Geeks Classes" );
Console.WriteLine( "The number of elements in mySortedSet are: "
+ mySortedSet.Count);
}
}
|
Output:
The number of elements in mySortedSet are: 4
Reference:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
01 Feb, 2019
Like Article
Save Article