C# | Get the number of elements contained in SortedList
Last Updated :
01 Feb, 2019
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:
using System;
using System.Collections;
class Geeks {
public static void Main(String[] args)
{
SortedList fslist = new SortedList();
Console.WriteLine(fslist.Count);
}
}
|
Example 2:
using System;
using System.Collections;
class Geeks {
public static void Main(String[] args)
{
SortedList fslist = new SortedList();
Console.WriteLine(fslist.Count);
fslist.Add( "1" , "GFG" );
fslist.Add( "2" , "Geeks" );
fslist.Add( "3" , "for" );
fslist.Add( "4" , "Geeks" );
Console.WriteLine(fslist.Count);
}
}
|
Note:
- Key/value pair can be accessed as a DictionaryEntry object.
- The count is the number of elements which are actually present in the SortedList but capacity is the number of elements which can be stored by the SortedList object.
- Capacity is always greater than or equal to Count. If Count exceeds Capacity while adding elements, the capacity is automatically increased by reallocating the internal array before copying the old elements and adding the new elements.
- Retrieving the value of this property is an O(1) operation.
Reference:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...