Open In App

C# | Capacity of a List

List<T>.Capacity Property is used to gets or sets the total number of elements the internal data structure can hold without resizing.

Properties of List:



Capacity Vs Count:

Syntax:



public int Capacity { get; set; }

Return Value: This method returns the number of elements that the List<T> can contain before resizing is required of type System.Int32.

Exceptions:

Below programs illustrate the use of Capacity Property:

Example 1:




// C# program to illustrate the
// Capacity Property of List<T>
using System;
using System.Collections.Generic;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating a List of integers
        // Here we are not setting
        // Capacity explicitly
        List<int> firstlist = new List<int>();
  
        // adding elements in firstlist
        firstlist.Add(1);
        firstlist.Add(2);
        firstlist.Add(3);
        firstlist.Add(4);
  
        // Printing the Capacity of firstlist
        Console.WriteLine("Capacity Is: " + firstlist.Capacity);
  
        // Printing the Count of firstlist
        Console.WriteLine("Count Is: " + firstlist.Count);
  
        // Adding some more
        // elements in firstlist
        firstlist.Add(5);
        firstlist.Add(6);
  
        // Printing the Capacity of firstlist
        // It will give output 8 as internally
        // List is resized
        Console.WriteLine("Capacity Is: " + firstlist.Capacity);
  
        // Printing the Count of firstlist
        Console.WriteLine("Count Is: " + firstlist.Count);
    }
}

Output:

Capacity Is: 4
Count Is: 4
Capacity Is: 8
Count Is: 6

Example 2:




// C# program to illustrate the
// Capacity Property of List<T>
using System;
using System.Collections.Generic;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating a List of integers
        // Here we are setting Capacity
        // explicitly i.e.
        List<int> firstlist = new List<int>(10);
  
        // Printing the Capacity of firstlist
        Console.WriteLine("Capacity Is: " + firstlist.Capacity);
  
        // Printing the Count of firstlist
        Console.WriteLine("Count Is: " + firstlist.Count);
  
        // adding elements in firstlist
        firstlist.Add(1);
        firstlist.Add(2);
        firstlist.Add(3);
        firstlist.Add(4);
  
        // Printing the Capacity of firstlist
        Console.WriteLine("Capacity Is: " + firstlist.Capacity);
  
        // Printing the Count of firstlist
        Console.WriteLine("Count Is: " + firstlist.Count);
  
        // Adding some more
        // elements in firstlist
        firstlist.Add(5);
        firstlist.Add(6);
  
        // Printing the Capacity of firstlist
        // It will give output 10 as we have
        // already set the Capacity
        Console.WriteLine("Capacity Is: " + firstlist.Capacity);
  
        // Printing the Count of firstlist
        Console.WriteLine("Count Is: " + firstlist.Count);
    }
}

Output:

Capacity Is: 10
Count Is: 0
Capacity Is: 10
Count Is: 4
Capacity Is: 10
Count Is: 6

Reference:


Article Tags :
C#