stride3d/stride · error · ArgumentOutOfRangeException

Capacity must be greater than 0.

Error message

Capacity must be greater than 0.

What it means

Stride's Deque<T> is a ring-buffer deque; its internal indexing requires a buffer of at least one element. The capacity constructor validates this and throws ArgumentOutOfRangeException when capacity < 1, because a zero or negative capacity cannot back the ring buffer.

Solutions

  1. Pass a capacity of at least 1 when constructing the deque.
  2. Clamp: Math.Max(1, requestedCapacity).
  3. Use the parameterless Deque<T>() constructor when no specific capacity is needed.

Example fix

// before
var deque = new Deque<Entity>(items.Count - 1); // 0 when items is empty

// after
var deque = new Deque<Entity>(Math.Max(1, items.Count));
Defensive patterns

Strategy: validation

Validate before calling

int capacity = ComputeCapacity();
if (capacity < 1)
    capacity = 1;
var deque = new Deque<Entity>(capacity);

Prevention

When it happens

Trigger: new Deque<T>(0) — e.g. default(int) or an uninitialized config value used as capacity; a computed capacity such as items.Count - 1 that evaluates to 0 on empty input.

Common situations: Pooling code sizing the deque from an empty source collection; configuration-driven initial capacities defaulting to 0; refactors that changed a 'size' parameter into a 'capacity' parameter.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/f5148ed2c024f461. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core/Collections/Dequeue.cs:74

    /// <summary>
    /// The offset into <see cref="buffer"/> where the view begins.
    /// </summary>
    private int offset;

    /// <summary>
    /// Used to wrap around indices when incrementing outside buffer range
    /// </summary>
    private int mask;

    /// <summary>
    /// Initializes a new instance of the <see cref="Deque&lt;T&gt;"/> class with the specified capacity.
    /// </summary>
    /// <param name="capacity">The initial capacity. Must be a power of two greater than <c>0</c>.</param>
    public Deque(int capacity)
    {
        if (capacity < 1)
            throw new ArgumentOutOfRangeException(nameof(capacity), "Capacity must be greater than 0.");

        if (int.IsPow2(capacity) == false)
            throw new InvalidOperationException("Capacity must be a power of two");

        buffer = new T[capacity];
        mask = buffer.Length - 1;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="Deque&lt;T&gt;"/> class.
    /// </summary>
    public Deque()
        : this(DefaultCapacity)
    {
    }

    #region GenericListImplementations

View on GitHub (pinned to 96fad776d2)