Open In App

C# | Capacity of a SortedList

SortedList.Capacity Property is used to get or set the capacity of a SortedList object.

Syntax:



public virtual int Capacity { get; set; }

Return Value: This property returns the number of elements of type System.Int32 that the SortedList object can contain.

Exceptions:



Capacity Vs Count:

Below given are some examples to understand the implementation in a better way:

Example 1:




// C# program to illustrate the
// Capacity Property of SortedList
using System;
using System.Collections;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating a SortedList of integers
        // Here we are not setting
        // Capacity explicitly
        SortedList mySortedList = new SortedList();
  
        // Adding elements to SortedList
        mySortedList.Add("1", "C#");
        mySortedList.Add("2", "Java");
        mySortedList.Add("3", "DSA");
        mySortedList.Add("4", "Python");
        mySortedList.Add("5", "C");
  
        // Printing the Capacity of mySortedList
        Console.WriteLine("Capacity Is: " + mySortedList.Capacity);
  
        // Printing the Count of mySortedList
        Console.WriteLine("Count Is: " + mySortedList.Count);
  
        // Adding some more
        // elements in mySortedList
        mySortedList.Add("6", "C++");
        mySortedList.Add("7", "HTML");
        mySortedList.Add("8", "CSS");
        mySortedList.Add("9", "Web");
        mySortedList.Add("10", "DBMS");
        mySortedList.Add("11", "CN");
        mySortedList.Add("12", "Ruby");
        mySortedList.Add("13", "Perl");
        mySortedList.Add("14", "R");
        mySortedList.Add("15", "GO");
        mySortedList.Add("16", "Scala");
        mySortedList.Add("17", "Big Data");
  
        // Printing the Capacity of mySortedList
        // It will give output 32 as internally
        // SortedList is resized
        Console.WriteLine("Capacity Is: " + mySortedList.Capacity);
  
        // Printing the Count of mySortedList
        Console.WriteLine("Count Is: " + mySortedList.Count);
    }
}

Output:

Capacity Is: 16
Count Is: 5
Capacity Is: 32
Count Is: 17

Example 2:




// C# program to illustrate the
// Capacity Property of SortedList
using System;
using System.Collections;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating a SortedList of integers
        // Here we are setting Capacity
        // explicitly i.e. 7
        SortedList mySortedList = new SortedList(7);
  
        // Printing the Capacity of mySortedList
        Console.WriteLine("Capacity Is: " + mySortedList.Capacity);
  
        // Printing the Count of mySortedList
        Console.WriteLine("Count Is: " + mySortedList.Count);
  
        // Adding elements to SortedList
        mySortedList.Add("1", "C#");
        mySortedList.Add("2", "Java");
        mySortedList.Add("3", "DSA");
        mySortedList.Add("4", "Python");
        mySortedList.Add("5", "C");
  
        // Printing the Capacity of mySortedList
        Console.WriteLine("Capacity Is: " + mySortedList.Capacity);
  
        // Printing the Count of mySortedList
        Console.WriteLine("Count Is: " + mySortedList.Count);
  
        // Adding some more
        // elements in firstlist
        mySortedList.Add("6", "C++");
        mySortedList.Add("7", "HTML");
        mySortedList.Add("8", "CSS");
        mySortedList.Add("9", "Web");
  
        // Printing the Capacity of mySortedList
        // It will give output 14 as internally
        // SortedList is resized
        Console.WriteLine("Capacity Is: " + mySortedList.Capacity);
  
        // Printing the Count of firstlist
        Console.WriteLine("Count Is: " + mySortedList.Count);
    }
}

Output:

Capacity Is: 7
Count Is: 0
Capacity Is: 7
Count Is: 5
Capacity Is: 14
Count Is: 9

Reference:


Article Tags :
C#