In C#, List.EnsureCapacity(capacity) prepares the list to hold at least that many items. It reserves the room now, so that a later run of Add() calls does not have to grow the hidden array again and again.
Remember that a list has two numbers: Count (how many items it holds) and Capacity (how many it can hold before growing). EnsureCapacity raises only the second one. It never adds items, and it never makes the list smaller. It is a speed and memory tip, not a change in what the list contains.
This is an instance member of List<T> (System.Collections.Generic). Call it on a list variable, such as numbers.EnsureCapacity(...). A List<T> is a class, so two variables can share one list. It keeps items in the order you put them, allows duplicates, and grows when needed. An array, by comparison, has a fixed length. List<T>.EnsureCapacity was added in newer .NET versions (around .NET 6). It is not in .NET Framework, so check your project version. On older versions, set the Capacity property or use the constructor that takes a capacity instead.
Syntax
Pass the number of items you expect. The method returns the capacity the list has afterwards.
C#
List<int> numbers = new List<int>();
int cap = numbers.EnsureCapacity(100); // cap is at least 100
// numbers.Count is still 0
Call
What it does
numbers.EnsureCapacity(100)
Makes room for at least 100 items and returns the real capacity (100 or more).
numbers.EnsureCapacity(0)
Allowed. Nothing special happens, and it returns the current capacity.
new List<int>(100)
Older way: ask for the capacity in the constructor, so the list starts with room for 100.
The returned capacity can be bigger than the number you passed, because the list may round up when it grows. If the list already has enough room, nothing changes. A negative number throws ArgumentOutOfRangeException. The exact growth rule is an internal detail, so print comparisons such as cap >= 100, and not the capacity number itself.
Library declarations
C#
public int EnsureCapacity(int capacity);
Parameters
Parameter
Description
capacity
Required. The number of items the list must be able to hold. It must be 0 or more.
Return Value
Return Value
Description
int
The capacity of the list after the call. It is at least the number you asked for. Do not depend on the exact value.
C# Examples
Example 1 — Reserve Room for 100 Items
Call EnsureCapacity on an empty list. Room is reserved, but no item exists yet.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> numbers = new List<int>();
int cap = numbers.EnsureCapacity(100);
Console.WriteLine(cap >= 100);
Console.WriteLine(numbers.Capacity >= 100);
Console.WriteLine(numbers.Count);
}
}
Output
True
True
0
How It Works
Room first. The list asks for a hidden array that can hold at least 100 numbers.
Return value.cap is the new capacity. We print only cap >= 100, since the exact number is an internal detail.
Property agrees.numbers.Capacity reports the same room, so the second comparison is True as well.
No items yet.Count is still 0. Reserving room does not create items.
Example 2 — Reserve Before a Big Load
Reserve room for 1000 items, then add 1000 items. The capacity does not have to grow, which saves copying.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> data = new List<int>();
data.EnsureCapacity(1000);
int before = data.Capacity;
for (int i = 0; i < 1000; i++) {
data.Add(i);
}
Console.WriteLine(data.Count);
Console.WriteLine(data.Capacity == before);
}
}
Output
1000
True
How It Works
Plan ahead. The program knows it will add 1000 items, so it reserves that much room before the loop.
Remember the room.before stores the capacity so it can be compared later. The number itself is never printed.
No growth inside the loop. Each Add finds a free slot, so the list never allocates a bigger array and never copies.
Same capacity at the end. After 1000 adds, Capacity still equals before, so the comparison is True.
Example 3 — EnsureCapacity Never Shrinks
Ask for less room than the list already has: nothing changes. A negative number is an error.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> list = new List<int>(50);
int before = list.Capacity;
list.EnsureCapacity(10);
Console.WriteLine(list.Capacity == before);
try {
list.EnsureCapacity(-1);
} catch (ArgumentOutOfRangeException) {
Console.WriteLine("capacity cannot be negative");
}
Console.WriteLine(list.Count);
}
}
Output
True
capacity cannot be negative
0
How It Works
Start with 50. The constructor new List<int>(50) asks for room for 50 items. before stores it.
Less is ignored.EnsureCapacity(10) asks for less than the list already has, so nothing changes and the first line is True.
Negative is an error.EnsureCapacity(-1) throws ArgumentOutOfRangeException. The catch block prints a message.
Count untouched. The list never held an item, so Count is still 0 after both calls.
Frequently Asked Questions
It makes sure the list can hold at least the number of items you give, without growing again. It returns the capacity after the call and never changes Count.
It was added in newer .NET (around .NET 6) and is not in .NET Framework. On older versions use the constructor new List<T>(capacity) or set the Capacity property. Check the Microsoft docs for your target.
Count is how many items the list holds. Capacity is how many it can hold before it must allocate a bigger internal array.
No. If the list already has enough room, nothing happens. Use TrimExcess to give unused room back.
When you know you will add many thousands of items. For small lists the saving is too small to notice.