{"record":{"id":"01c800f904e4b921","repo":"stride3d/stride","slug":"capacity-too-small","errorCode":null,"errorMessage":"capacity too small","messagePattern":"capacity too small","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core/Collections/SortedList.cs","lineNumber":169,"sourceCode":"        {\n            this[ToKey(key)] = ToValue(value);\n        }\n    }\n\n    public int Capacity\n    {\n        get\n        {\n            return table.Length;\n        }\n\n        set\n        {\n            var current = this.table.Length;\n\n            if (inUse > value)\n            {\n                throw new ArgumentOutOfRangeException(\"capacity too small\");\n            }\n            if (value == 0)\n            {\n                // return to default size\n                var newTable = new KeyValuePair<TKey, TValue>[defaultCapacity];\n                Array.Copy(table, newTable, inUse);\n                this.table = newTable;\n            }\n#if NET_1_0\n            else if (current > defaultCapacity && value < current) {\n                                    KeyValuePair<TKey, TValue> [] newTable = new KeyValuePair<TKey, TValue> [defaultCapacity];\n                                    Array.Copy (table, newTable, inUse);\n                                    this.table = newTable;\n                            }\n#endif\n            else if (value > inUse)\n            {\n                var newTable = new KeyValuePair<TKey, TValue>[value];","sourceCodeStart":151,"sourceCodeEnd":187,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core/Collections/SortedList.cs#L151-L187","documentation":"SortedList's Capacity setter throws ArgumentOutOfRangeException with message 'capacity too small' when the requested value is less than the number of elements currently in use (inUse); shrinking capacity below the current item count would lose data. The message is misleadingly non-standard for the exception type.","triggerScenarios":"Setting Capacity to a value < the list's current Count while it holds items; trimming capacity aggressively after expecting an empty collection; setting Capacity = 0 while items remain in the list.","commonSituations":"Memory-tuning code that shrinks capacity after removals without checking Count first; config-driven capacity values smaller than loaded data; reusing a list instance that still contains items.","solutions":["Clear the list (or remove enough items) before shrinking Capacity below inUse","Only set Capacity >= list Count, e.g. if (newCap >= list.Count) list.Capacity = newCap;","Trim capacity only when Count == 0 or use Capacity = Math.Max(desired, Count)","Catch ArgumentOutOfRangeException if capacity is advisory and clamp the value instead"],"exampleFix":"// before\nlist.Capacity = 4; // throws if list.Count > 4\n// after\nif (list.Count == 0)\n    list.Capacity = 4;\nelse\n    list.Capacity = Math.Max(4, list.Count);","handlingStrategy":"validation","validationCode":"if (desiredCapacity >= list.Count) list.Capacity = desiredCapacity;","typeGuard":null,"tryCatchPattern":"try { list.Capacity = newCap; } catch (ArgumentOutOfRangeException) { list.Capacity = Math.Max(newCap, list.Count); }","preventionTips":["Check Count before shrinking Capacity","Clear the list before aggressive capacity reduction","Treat Capacity as a hint, never below current item count"],"tags":["csharp","collections","capacity","argument-out-of-range"],"backgroundTag":"argument-out-of-range","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"}