Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the Capacity property of an ArrayList class in C#?
The Capacity property in the ArrayList class gets or sets the number of elements that the ArrayList can contain. This property represents the internal array size, which is automatically managed by the ArrayList to optimize performance.
The capacity is always greater than or equal to the count of elements. When elements are added and the capacity is exceeded, the ArrayList automatically doubles its capacity to accommodate more elements.
Syntax
Following is the syntax for accessing the Capacity property −
arrayListName.Capacity
You can also set the capacity explicitly −
arrayListName.Capacity = newCapacityValue;
How Capacity Works
The default initial capacity of an ArrayList is 4. When the number of elements exceeds the current capacity, the ArrayList automatically doubles its capacity. This doubling strategy minimizes the number of memory reallocations needed as the list grows.
Example
The following example demonstrates how the Capacity property works with different numbers of elements −
using System;
using System.Collections;
class Demo {
public static void Main() {
ArrayList arrList = new ArrayList();
arrList.Add(19);
arrList.Add(44);
arrList.Add(22);
ArrayList arrList2 = new ArrayList();
arrList2.Add(19);
arrList2.Add(44);
arrList2.Add(64);
arrList2.Add(32);
arrList2.Add(99);
Console.WriteLine("ArrayList1 - Total elements: " + arrList.Count);
Console.WriteLine("ArrayList1 - Capacity: " + arrList.Capacity);
Console.WriteLine("ArrayList2 - Total elements: " + arrList2.Count);
Console.WriteLine("ArrayList2 - Capacity: " + arrList2.Capacity);
}
}
The output of the above code is −
ArrayList1 - Total elements: 3 ArrayList1 - Capacity: 4 ArrayList2 - Total elements: 5 ArrayList2 - Capacity: 8
Setting Capacity Explicitly
You can set the capacity explicitly to optimize performance when you know the approximate number of elements −
using System;
using System.Collections;
class Demo {
public static void Main() {
ArrayList arrList = new ArrayList();
Console.WriteLine("Initial capacity: " + arrList.Capacity);
// Set capacity to 10
arrList.Capacity = 10;
Console.WriteLine("After setting capacity to 10: " + arrList.Capacity);
// Add elements
for (int i = 1; i <= 5; i++) {
arrList.Add(i * 10);
}
Console.WriteLine("Count: " + arrList.Count);
Console.WriteLine("Capacity remains: " + arrList.Capacity);
}
}
The output of the above code is −
Initial capacity: 0 After setting capacity to 10: 10 Count: 5 Capacity remains: 10
Conclusion
The Capacity property in ArrayList represents the internal array size and automatically doubles when exceeded. Understanding capacity helps optimize memory usage and performance, especially when you can estimate the number of elements in advance.
