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
Get or set the number of elements that the ArrayList can contain in C#
The Capacity property of an ArrayList in C# gets or sets the number of elements that the ArrayList can contain without resizing. This is different from the Count property, which represents the actual number of elements currently stored in the ArrayList.
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 getting the capacity −
int capacity = arrayList.Capacity;
Following is the syntax for setting the capacity −
arrayList.Capacity = newCapacity;
Understanding Capacity vs Count
Using Default Capacity
When no initial capacity is specified, ArrayList starts with a default capacity and automatically expands as needed −
using System;
using System.Collections;
public class Demo {
public static void Main() {
ArrayList arrList = new ArrayList();
arrList.Add(25);
arrList.Add(50);
arrList.Add(75);
arrList.Add(100);
arrList.Add(125);
arrList.Add(150);
arrList.Add(175);
arrList.Add(200);
arrList.Add(225);
arrList.Add(250);
Console.WriteLine("Elements in ArrayList...");
foreach(int i in arrList) {
Console.WriteLine(i);
}
Console.WriteLine("Count of elements = " + arrList.Count);
Console.WriteLine("Capacity = " + arrList.Capacity);
}
}
The output of the above code is −
Elements in ArrayList... 25 50 75 100 125 150 175 200 225 250 Count of elements = 10 Capacity = 16
Setting Initial Capacity
You can specify an initial capacity when creating the ArrayList to optimize memory usage −
using System;
using System.Collections;
public class Demo {
public static void Main() {
ArrayList arrList = new ArrayList(10);
arrList.Add(25);
arrList.Add(50);
arrList.Add(75);
arrList.Add(100);
Console.WriteLine("Elements in ArrayList...");
foreach(int i in arrList) {
Console.WriteLine(i);
}
Console.WriteLine("Count of elements = " + arrList.Count);
Console.WriteLine("Capacity = " + arrList.Capacity);
}
}
The output of the above code is −
Elements in ArrayList... 25 50 75 100 Count of elements = 4 Capacity = 10
Modifying Capacity at Runtime
The capacity can be changed dynamically during program execution −
using System;
using System.Collections;
public class Demo {
public static void Main() {
ArrayList arrList = new ArrayList();
arrList.Add("Apple");
arrList.Add("Banana");
Console.WriteLine("Initial Capacity: " + arrList.Capacity);
Console.WriteLine("Initial Count: " + arrList.Count);
arrList.Capacity = 20;
Console.WriteLine("After setting Capacity to 20: " + arrList.Capacity);
Console.WriteLine("Count remains: " + arrList.Count);
}
}
The output of the above code is −
Initial Capacity: 4 Initial Count: 2 After setting Capacity to 20: 20 Count remains: 2
Conclusion
The Capacity property controls the internal storage size of an ArrayList, while Count represents the actual number of elements. Setting an appropriate initial capacity can improve performance by reducing memory reallocations as elements are added.
