Open In App

C# | Get the number of elements in the SortedSet

Last Updated : 01 Feb, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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:




// C# code to get the number of
// elements in the 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(1);
        mySortedSet.Add(2);
        mySortedSet.Add(3);
        mySortedSet.Add(4);
        mySortedSet.Add(5);
  
        // Displaying the number of elements in
        // the SortedSet using "Count" function
        Console.WriteLine("The number of elements in mySortedSet are: " 
                                                  + mySortedSet.Count);
    }
}


Output:

The number of elements in mySortedSet are: 5

Example 2:




// C# code to get the number of
// elements in the SortedSet
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a SortedSet of strings
        SortedSet<string> mySortedSet = new SortedSet<string>();
  
        // adding elements in mySortedSet
        mySortedSet.Add("Hey");
        mySortedSet.Add("GeeksforGeeks");
        mySortedSet.Add("and");
        mySortedSet.Add("Geeks Classes");
  
        // Displaying the number of elements in
        // the SortedSet using "Count" function
        Console.WriteLine("The number of elements in mySortedSet are: " 
                                                   + mySortedSet.Count);
    }
}


Output:

The number of elements in mySortedSet are: 4

Reference:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads