{"record":{"id":"423cc921aebeff67","repo":"dotnet/wpf","slug":"sr-tablecollectionnotenoughcapacity","errorCode":null,"errorMessage":"SR.TableCollectionNotEnoughCapacity","messagePattern":"SR\\.TableCollectionNotEnoughCapacity","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/ContentElementCollection.cs","lineNumber":757,"sourceCode":"\n        #region Internal Properties\n\n        /// <summary>\n        /// PrivateCapacity sets/gets the Capacity of the collection.\n        /// </summary>\n        internal int PrivateCapacity\n        {\n            get\n            {\n                return (Items.Length);\n            }\n            set\n            {\n                if (value != Items.Length)\n                {\n                    if (value < Size)\n                    {\n                        throw new ArgumentOutOfRangeException(SR.TableCollectionNotEnoughCapacity);\n                    }\n\n                    if (value > 0)\n                    {\n                        TItem[] newItems = new TItem[value];\n                        if (Size > 0)\n                        {\n                            Array.Copy(Items, 0, newItems, 0, Size);\n                        }\n                        Items = newItems;\n                    }\n                    else\n                    {\n                        Items = new TItem[DefaultCapacity];\n                    }\n                }\n            }\n        }","sourceCodeStart":739,"sourceCodeEnd":775,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/ContentElementCollection.cs#L739-L775","documentation":"The Capacity property setter throws ArgumentOutOfRangeException with SR.TableCollectionNotEnoughCapacity when the requested capacity is less than the current number of items (Size). Shrinking capacity below the live element count would orphan elements, so it is rejected.","triggerScenarios":"Setting Capacity (or TrimToSize-like paths) to a value < Size, e.g. assigning 0 while the collection still contains items.","commonSituations":"Calling TrimToSize() on a non-empty collection where it maps to a capacity of Size but custom logic passes 0, or pre-sizing a fresh collection and later trying to shrink it without clearing first.","solutions":["Clear the collection (or remove items) before setting a smaller Capacity","Set Capacity to a value >= collection.Size","Only shrink when Size == 0, or let the library's TrimToSize handle it"],"exampleFix":"// before\ncollection.Capacity = 0;\n// after\nif (collection.Size == 0) { collection.Capacity = 0; } else { collection.Clear(); collection.Capacity = 0; }","handlingStrategy":"validation","validationCode":"if (newCapacity >= collection.Size) collection.Capacity = newCapacity;","typeGuard":null,"tryCatchPattern":"try { collection.Capacity = value; } catch (ArgumentOutOfRangeException) { /* clear items or choose a larger capacity */ }","preventionTips":["Never set Capacity below the current item count","Clear the collection before shrinking capacity","Let TrimToSize handle capacity reduction"],"tags":["argument-out-of-range","wpf","collections","capacity"],"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"}