{"record":{"id":"27dc3d419d00334a","repo":"stride3d/stride","slug":"capacity-cannot-be-set-to-a-value-less-than-count","errorCode":null,"errorMessage":"Capacity cannot be set to a value less than Count","messagePattern":"Capacity cannot be set to a value less than Count","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core/Collections/Dequeue.cs","lineNumber":469,"sourceCode":"\n    /// <summary>\n    /// 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\"/>.\n    /// </summary>\n    /// <exception cref=\"InvalidOperationException\"><c>Capacity</c> cannot be set to a value less than <see cref=\"Count\"/>.</exception>\n    public int Capacity\n    {\n        get\n        {\n            return buffer.Length;\n        }\n\n        set\n        {\n            if (value < 1)\n                throw new ArgumentOutOfRangeException(nameof(value), \"Capacity must be greater than 0.\");\n\n            if (value < Count)\n                throw new InvalidOperationException(\"Capacity cannot be set to a value less than Count\");\n\n            if (int.IsPow2(value) == false)\n                throw new InvalidOperationException(\"Capacity must be a power of two\");\n\n            if (value == buffer.Length)\n                return;\n\n            // Create the new buffer and copy our existing range.\n            T[] newBuffer = new T[value];\n            var newSpan = newBuffer.AsSpan();\n            if (WrapsAround(offset, Count, out var splitA, out var splitB))\n            {\n                // The existing buffer is split, so we have to copy it in parts\n                splitA.CopyTo(newSpan[.. splitA.Length]);\n                splitB.CopyTo(newSpan[splitA.Length .. (splitA.Length + splitB.Length)]);\n            }\n            else\n            {","sourceCodeStart":451,"sourceCodeEnd":487,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core/Collections/Dequeue.cs#L451-L487","documentation":"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.","triggerScenarios":"Setting Capacity to any value < deque.Count, e.g. Capacity = 8 while the deque holds 20 items.","commonSituations":"Shrinking capacity to 'save memory' without first removing elements; restoring a default capacity after bulk loads; copying a capacity value from another, smaller deque.","solutions":["Remove enough elements (RemoveFromBack/RemoveRange/Clear) so Count <= desired capacity first","Choose a power-of-two capacity >= Count (e.g. round up to the next power of two)","Only lower Capacity after verifying deque.Count"],"exampleFix":"// before\ndeque.Capacity = 16; // Count is 20 -> throws\n// after\nwhile (deque.Count > 16) deque.RemoveFromBack();\ndeque.Capacity = 16;","handlingStrategy":"validation","validationCode":"if (desired >= deque.Count) deque.Capacity = desired;","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never shrink Capacity below Count — trim elements first","Round desired capacity up to a power of two >= Count","Recheck Count immediately before assigning Capacity"],"tags":["collections","deque","capacity","invalid-operation"],"backgroundTag":"invalid-state-transition","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}