dotnet/wpf · error
SR.TableCollectionNotEnoughCapacity
Error message
SR.TableCollectionNotEnoughCapacity
What it means
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.
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
Example fix
// before
collection.Capacity = 0;
// after
if (collection.Size == 0) { collection.Capacity = 0; } else { collection.Clear(); collection.Capacity = 0; } Defensive patterns
Strategy: validation
Validate before calling
if (newCapacity >= collection.Size) collection.Capacity = newCapacity;
Try / catch
try { collection.Capacity = value; } catch (ArgumentOutOfRangeException) { /* clear items or choose a larger capacity */ } Prevention
- Never set Capacity below the current item count
- Clear the collection before shrinking capacity
- Let TrimToSize handle capacity reduction
When it happens
Trigger: Setting Capacity (or TrimToSize-like paths) to a value < Size, e.g. assigning 0 while the collection still contains items.
Common situations: 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.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- SR.Format(SR.FrugalList_TargetMapCannotHoldAllData…
- SR.TableCollectionOutOfRange
- SR.VisualCollection_NotEnoughCapacity
- throw new ArgumentOutOfRangeException(nameof(index));
- args
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/423cc921aebeff67.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/ContentElementCollection.cs:757
#region Internal Properties
/// <summary>
/// PrivateCapacity sets/gets the Capacity of the collection.
/// </summary>
internal int PrivateCapacity
{
get
{
return (Items.Length);
}
set
{
if (value != Items.Length)
{
if (value < Size)
{
throw new ArgumentOutOfRangeException(SR.TableCollectionNotEnoughCapacity);
}
if (value > 0)
{
TItem[] newItems = new TItem[value];
if (Size > 0)
{
Array.Copy(Items, 0, newItems, 0, Size);
}
Items = newItems;
}
else
{
Items = new TItem[DefaultCapacity];
}
}
}
}View on GitHub (pinned to 81131a70a4)