TheAlgorithms/C-Sharp · error · ArgumentException
Capacity must be at least 1.
Error message
Capacity must be at least 1.
What it means
The Deque<T>(int capacity) constructor requires an initial capacity of at least 1. Passing 0 or a negative value cannot create the backing array, so an ArgumentException naming the 'capacity' parameter is thrown immediately at construction time.
Solutions
- Pass a positive capacity, e.g. new Deque<T>(Math.Max(1, requestedCapacity)).
- If the initial size is unknown, use a small positive default (e.g. 4 or 16).
- Validate the capacity at the call site before constructing and surface a clearer domain error.
- If the capacity comes from config, clamp it: capacity = Math.Max(1, parsedCapacity).
Example fix
// before var deque = new Deque<int>(items.Count); // throws when items is empty (0) // after var deque = new Deque<int>(Math.Max(1, items.Count));
Defensive patterns
Strategy: validation
Validate before calling
void EnsureCapacity(int c) { if (c < 1) throw new ArgumentOutOfRangeException(nameof(c), "Capacity must be at least 1."); } Try / catch
try { var d = new Deque<T>(capacity); } catch (ArgumentException ex) when (ex.ParamName == "capacity") { capacity = 1; d = new Deque<T>(capacity); } Prevention
- Always clamp computed capacities: Math.Max(1, count).
- Never pass default(int) (0) straight into the constructor.
- Validate config-sourced capacity values at startup.
- Default to a small positive capacity (e.g. 8) when size is unknown.
When it happens
Trigger: new Deque<int>(0) or new Deque<T>(-1) — any capacity argument less than 1, often from an unvalidated config value, a computed size of 0 for empty input, or a default(int) of 0.
Common situations: Passing a computed count that is 0 when the collection is empty; reading capacity from configuration where the key is missing/0; using default parameter values without validating them.
Related errors
- The sequence may only contain ones or zeros
- Deque is empty.
- Matrix must be square!
- key is not in the tree
- Tree is empty!
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/dc9ebde534cc3e1e.
Report an issue: GitHub.
Appendix: source
Thrown at DataStructures/Deque/Deque.cs:56
/// Initializes a new instance of the <see cref="Deque{T}" /> class with default capacity.
/// Default capacity is 16 elements, which provides a good balance between
/// memory usage and avoiding early resizing for typical use cases.
/// </summary>
public Deque()
: this(16)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Deque{T}" /> class with specified capacity.
/// </summary>
/// <param name="capacity">The initial capacity of the deque.</param>
/// <exception cref="ArgumentException">Thrown when capacity is less than 1.</exception>
public Deque(int capacity)
{
if (capacity < 1)
{
throw new ArgumentException("Capacity must be at least 1.", nameof(capacity));
}
items = new T[capacity];
front = 0;
rear = 0;
count = 0;
}
/// <summary>
/// Gets the number of elements in the deque.
/// </summary>
public int Count => count;
/// <summary>
/// Gets a value indicating whether the deque is empty.
/// </summary>
public bool IsEmpty => count == 0;
View on GitHub (pinned to 96e2905cab)