Open In App

C# | Capacity of a SortedList

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

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:

  • ArgumentOutOfRangeException: If the value assigned is less than the current number of elements in the SortedList object.
  • OutOfMemoryException: If there is not enough memory available on the system.

Capacity Vs Count:

  • Count is always less than the Capacity. While adding the elements, if Count exceeds Capacity then the Capacity will increase automatically by reallocating the internal array before copying the old elements and adding the new elements.
  • Capacity is the number of the elements which the List can store before resizing of List needed. But Count is the number of the elements which are actually present in the List.
  • If the Capacity is much larger than the Count the user can decrease capacity by either calling the TrimExcess method or explicitly setting the Capacity to a lower value.
  • If the Capacity is settled explicitly then the internal array is also reallocated to accommodate the specified capacity, and all the elements are copied.
  • Retrieving the value of the Capacity property is an O(1) operation while setting the Capacity is an O(n) operation, where n is a new capacity.

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:



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

Similar Reads