stride3d/stride · error · InvalidOperationException

Capacity cannot be set to a value less than Count

Error message

Capacity cannot be set to a value less than Count

What it means

Thrown by the Deque.Capacity setter when the new capacity is smaller than the deque's current Count. The buffer cannot hold the existing elements at that size, so the assignment is refused with InvalidOperationException.

Solutions

  1. Remove enough elements (RemoveFromBack/RemoveRange/Clear) so Count <= desired capacity first
  2. Choose a power-of-two capacity >= Count (e.g. round up to the next power of two)
  3. Only lower Capacity after verifying deque.Count

Example fix

// before
deque.Capacity = 16; // Count is 20 -> throws
// after
while (deque.Count > 16) deque.RemoveFromBack();
deque.Capacity = 16;
Defensive patterns

Strategy: validation

Validate before calling

if (desired >= deque.Count) deque.Capacity = desired;

Prevention

When it happens

Trigger: Setting Capacity to any value < deque.Count, e.g. Capacity = 8 while the deque holds 20 items.

Common situations: Shrinking capacity to 'save memory' without first removing elements; restoring a default capacity after bulk loads; copying a capacity value from another, smaller deque.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

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

    /// <summary>
    /// Gets or sets the capacity for this deque. This value must always be greater than zero, and this property cannot be set to a value less than <see cref="Count"/>.
    /// </summary>
    /// <exception cref="InvalidOperationException"><c>Capacity</c> cannot be set to a value less than <see cref="Count"/>.</exception>
    public int Capacity
    {
        get
        {
            return buffer.Length;
        }

        set
        {
            if (value < 1)
                throw new ArgumentOutOfRangeException(nameof(value), "Capacity must be greater than 0.");

            if (value < Count)
                throw new InvalidOperationException("Capacity cannot be set to a value less than Count");

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

            if (value == buffer.Length)
                return;

            // Create the new buffer and copy our existing range.
            T[] newBuffer = new T[value];
            var newSpan = newBuffer.AsSpan();
            if (WrapsAround(offset, Count, out var splitA, out var splitB))
            {
                // The existing buffer is split, so we have to copy it in parts
                splitA.CopyTo(newSpan[.. splitA.Length]);
                splitB.CopyTo(newSpan[splitA.Length .. (splitA.Length + splitB.Length)]);
            }
            else
            {

View on GitHub (pinned to 96fad776d2)