{"record":{"id":"b827fb927f1ec959","repo":"dotnet/wpf","slug":"sr-visualcollection-notenoughcapacity","errorCode":null,"errorMessage":"SR.VisualCollection_NotEnoughCapacity","messagePattern":"SR\\.VisualCollection_NotEnoughCapacity","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/VisualCollection.cs","lineNumber":246,"sourceCode":"        }\n\n        /// <summary>\n        /// InternalCapacity sets/gets the Capacity of the collection.\n        /// </summary>\n        internal int InternalCapacity\n        {\n            get\n            {\n                return _items != null ? _items.Length : 0;\n            }\n            set\n            {\n                int currentCapacity = _items != null ? _items.Length : 0;\n                if (value != currentCapacity)\n                {\n                    if (value < _size)\n                    {\n                        throw new ArgumentOutOfRangeException(nameof(value), SR.VisualCollection_NotEnoughCapacity);\n                    }\n                    if (value > 0)\n                    {\n                        Visual[] newItems = new Visual[value];\n                        if (_size > 0)\n                        {\n                            Debug.Assert(_items != null);\n                            Array.Copy(_items, 0, newItems, 0, _size);\n                        }\n                        _items = newItems;\n                    }\n                    else\n                    {\n                        Debug.Assert(value == 0, \"There shouldn't be a case where value != 0.\");\n                        Debug.Assert(_size == 0, \"Size must be 0 here.\");\n                        _items = null;\n                    }\n                }","sourceCodeStart":228,"sourceCodeEnd":264,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/VisualCollection.cs#L228-L264","documentation":"Thrown by the VisualCollection.Capacity property setter when the requested capacity is smaller than the current number of items in the collection (_size). Capacity cannot shrink below Count because existing children would be lost.","triggerScenarios":"Setting collection.Capacity to a value less than collection.Count, e.g. after adding children then trying Capacity = 0 or Capacity = oldCapacity while more visuals were added since.","commonSituations":"Pooling/optimization code that resets capacity to a constant without checking Count; trimming memory after reparenting visuals into the collection.","solutions":["Remove children first (Clear/Remove) until Count <= desired capacity, then set Capacity.","Set Capacity to at least collection.Count: Capacity = Math.Max(desired, collection.Count).","Skip the capacity adjustment when Count already exceeds the target.","Consider using TrimToSize-style patterns: only lower capacity when Count == 0."],"exampleFix":"// before\nvisualCollection.Clear();\nAddMoreVisuals(visualCollection, 10);\nvisualCollection.Capacity = 5; // ArgumentOutOfRangeException: needs >= 10\n// after\nvisualCollection.Capacity = Math.Max(5, visualCollection.Count);","handlingStrategy":"validation","validationCode":"int desired = 5;\nif (desired >= visualCollection.Count)\n    visualCollection.Capacity = desired;","typeGuard":"static bool CanSetCapacity(VisualCollection c, int value) => value >= c.Count;","tryCatchPattern":"try { collection.Capacity = value; }\ncatch (ArgumentOutOfRangeException) { /* value < Count; clamp or clear first */ }","preventionTips":["Always clamp capacity to Math.Max(desired, collection.Count).","Clear the collection before aggressively shrinking capacity.","Recalculate capacity from Count rather than stale constants."],"tags":["wpf","visual-collection","capacity","argument-out-of-range"],"backgroundTag":"value-out-of-range","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}