C# Queue EnsureCapacity() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Queue

Definition

In C#, Queue.EnsureCapacity(capacity) prepares the queue to hold at least that many items. It reserves the space now, so the queue does not have to allocate a bigger array again and again while you add items.

Think of booking a long bench before a crowd arrives. Nobody sits down yet; Count stays the same. It only makes later Enqueue() calls cheaper when you already know about how many items are coming. It is a speed and memory tip, never a change in what the queue contains.

This is an instance member of Queue<T> (System.Collections.Generic). Call it on a queue variable, such as line.EnsureCapacity(...). A Queue<T> is a class, so two variables can share one queue. It is first in, first out (FIFO): items leave in the order they arrived. It is not thread-safe; for several threads use ConcurrentQueue<T>. EnsureCapacity exists in newer .NET versions (it was added in recent .NET releases, around .NET 6). It is not in classic .NET Framework, so check your project version.

Syntax

Pass the number of items you expect. The method returns the capacity the queue now has.

C#
Queue<int> queue = new Queue<int>();
int cap = queue.EnsureCapacity(100);   // cap is at least 100
// queue.Count is still 0
CallWhat it does
queue.EnsureCapacity(100)Makes room for at least 100 items and returns the real capacity (100 or more).
queue.EnsureCapacity(0)Allowed. Nothing special happens; it returns the current capacity.
new Queue<int>(100)Another way: pick the starting capacity in the constructor, when you create the queue.
queue.TrimExcess()The opposite job: give back unused space. See TrimExcess().

The returned capacity can be bigger than the number you passed, because the queue may grow in steps. If it already has enough room, nothing changes. A negative number throws ArgumentOutOfRangeException. Queue<T> has no public Capacity property, so the return value is the way to see it, and the exact number is an internal detail.

Library declarations
C#
public int EnsureCapacity(int capacity);

Parameters

ParameterDescription
capacityRequired. The number of items the queue must be able to hold. It must be 0 or more.

Return Value

Return ValueDescription
intThe capacity of the queue after the call. It is at least the number you asked for. Do not print it or rely on its exact value in your own logic.

C# Examples

Example 1 — Reserve Room for 100 Items

Call EnsureCapacity on an empty queue and check that nothing was added.

C#
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        Queue<int> queue = new Queue<int>();
        int cap = queue.EnsureCapacity(100);

        Console.WriteLine(cap >= 100);
        Console.WriteLine(queue.Count);

        queue.Enqueue(7);
        Console.WriteLine(queue.Count);
    }
}

How It Works

  1. The return value. cap is the new capacity. The code prints only the comparison cap >= 100, because the exact number is an internal detail.
  2. Still empty. Reserving room adds no items, so Count is 0 right after the call.
  3. Enqueue as usual. After one Enqueue(7), Count is 1.
  4. Same behavior. A queue with reserved room acts like any other queue. Only the memory plan is different.

Example 2 — Load a Thousand Items After Reserving Room

Reserve space for 1000 items, then enqueue the numbers 0 to 999. Check the front and the count.

C#
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        Queue<int> queue = new Queue<int>();
        queue.EnsureCapacity(1000);

        for (int i = 0; i < 1000; i++) {
            queue.Enqueue(i);
        }

        Console.WriteLine(queue.Count);
        Console.WriteLine(queue.Peek());
        Console.WriteLine(queue.Contains(999));
    }
}

How It Works

  1. Reserve first. The call tells the queue that up to 1000 items may come, so it can allocate once instead of growing in several steps.
  2. One thousand adds. The loop calls Enqueue 1000 times, so Count ends at 1000.
  3. Front is the first number. The oldest item is 0, so Peek() prints 0.
  4. Last item present. Contains(999) is True, because 999 was the last number added to the back.

Example 3 — Order Stays, Smaller Requests, and Bad Values

Wrap the queue around by dequeuing and enqueuing, reserve space, and see that the order stays. Then try a smaller number and a negative one.

C#
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        Queue<int> queue = new Queue<int>(new int[] { 1, 2, 3 });
        queue.Dequeue();
        queue.Enqueue(4);

        int big = queue.EnsureCapacity(50);
        Console.WriteLine(big >= 50);
        Console.WriteLine(string.Join(", ", queue));

        int small = queue.EnsureCapacity(10);
        Console.WriteLine(small >= big);

        try {
            queue.EnsureCapacity(-1);
        } catch (ArgumentOutOfRangeException) {
            Console.WriteLine("negative capacity");
        }
    }
}

How It Works

  1. A shifted queue. After one Dequeue() and one Enqueue(4), the queue holds 2, 3, 4, and the items may sit in a wrapped-around way inside the internal array.
  2. Order is rebuilt correctly. Growing the array moves the items internally, but the printed order is still front to back: 2, 3, 4.
  3. Never shrinks. Asking for 10 when there is room for at least 50 does not reduce anything, so small >= big is True. To give space back, use TrimExcess.
  4. Negative is invalid. A capacity below 0 makes no sense, so it throws ArgumentOutOfRangeException and the catch block prints a message.

Frequently Asked Questions

It reserves room for at least the number of items you ask for, so the queue does not need to grow while you enqueue items. It does not add or remove any item.
It returns the capacity the queue has after the call. That is at least the number you passed, and may be a little bigger.
It is in newer .NET versions and is not in classic .NET Framework. The exact first version can differ from what you expect, so check the documentation for the version your project uses.
Yes. The constructor new Queue<int>(1000) takes a starting capacity. EnsureCapacity does the same job after the queue already exists.
Only when you enqueue a large number of items. For small queues you will not notice any difference, so it is optional.

Next: GetEnumerator()

Get an enumerator that walks the queue from front to back; foreach uses it for you.

GetEnumerator() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful