Open In App

C# | Get the number of elements contained in SortedList

SortedList.Count Property is used to get the number of elements contained in a SortedList object.

Syntax:



public virtual int Count { get; }

Property Value: The number of elements contained in the SortedList object.

Below programs illustrate the use of above discussed method:



Example 1:




// C# Program to count the number
// of elements in SortedList
using System;
using System.Collections;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating object of SortedList
        // fslist is the SortedList object
        SortedList fslist = new SortedList();
  
        // Count property is used to get the
        // number of key/value pairs in fslist
        // It will give 0 as no pairs are present
        Console.WriteLine(fslist.Count);
    }
}

Output:
0

Example 2:




// C# Program to count the number
// of elements in SortedList
using System;
using System.Collections;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating object of SortedList
        // fslist is the SortedList object
        SortedList fslist = new SortedList();
  
        // Count property is used to get the
        // number of key/value pairs in fslist
        // It will give 0 as no pairs are present
        Console.WriteLine(fslist.Count);
  
        // Adding key/value pairs in fslist
        fslist.Add("1", "GFG");
        fslist.Add("2", "Geeks");
        fslist.Add("3", "for");
        fslist.Add("4", "Geeks");
  
        // Count property is used to get the
        // number of key/value pairs in fslist
        // It will give output 4
        Console.WriteLine(fslist.Count);
    }
}

Output:
0
4

Note:

Reference:


Article Tags :
C#