dotnet/wpf · error · ArgumentOutOfRangeException
SR.TableCollectionCountNeedNonNegNum
Error message
SR.TableCollectionCountNeedNonNegNum
What it means
TableColumnCollectionInternal.RemoveRange validates its (index, count) arguments before removing a range of table columns. This ArgumentOutOfRangeException is thrown when the requested count is negative. The library rejects it up front because a negative count is never a valid removal request.
Solutions
- Compute the count before the call and clamp with Math.Max(0, count) or skip the call when count <= 0.
- Check the arithmetic that produces the count; ensure the source bounds are captured before mutation.
- If the range should extend to the end, pass the correct remaining size (Size - index) instead of a sentinel negative.
Example fix
// before
collection.RemoveRange(start, end - start); // throws if end < start
// after
if (end > start)
{
collection.RemoveRange(start, end - start);
} Defensive patterns
Strategy: validation
Validate before calling
if (count < 0)
throw new ArgumentOutOfRangeException(nameof(count));
if (count > 0)
collection.RemoveRange(index, count); Type guard
static bool IsValidRemoveRange(ICollection c, int index, int count) => index >= 0 && index < c.Count && count >= 0 && count <= c.Count - index;
Try / catch
try { collection.RemoveRange(index, count); }
catch (ArgumentOutOfRangeException ex) { /* ex.ParamName == "count"; log and skip */ } Prevention
- Never pass a computed difference without Math.Max(0, ...)
- Skip RemoveRange calls when count == 0
- Capture range bounds before any mutation of the collection
When it happens
Trigger: Calling RemoveRange on a TableColumnCollectionInternal (or a TableColumnCollection that routes to it) with a count computed from a subtraction or variable that evaluates to less than 0, e.g. RemoveRange(0, -1) or RemoveRange(2, itemsAfter - itemsBefore) when the delta is negative.
Common situations: Developers computing the count from bounds that were already updated, off-by-one subtractions (newLast - oldLast), or passing a default/uninitialized int that remained negative.
Related errors
- SR.TableCollectionCountNeedNonNegNum
- SR.TableCollectionOutOfRange
- SR.GridCollection_DestArrayInvalidLowerBound
- SR.TableCollectionCountNeedNonNegNum
- SR.TableCollectionRangeOutOfRange
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/d7afd0552b3de5d0.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/TableColumnCollectionInternal.cs:275
/// <exception cref="ArgumentException">
/// <c>index</c> and <c>count</c> do not denote a valid range of TItems in the ContentElementCollection.
/// </exception>
/// <remarks>
/// The TItems that follow the removed TItems move up to occupy
/// the vacated spot. The indices of the TItems that are moved are
/// also updated.
/// </remarks>
public override void RemoveRange(int index, int count)
{
Version++;
if (index < 0 || index >= Size)
{
throw new ArgumentOutOfRangeException(SR.TableCollectionOutOfRange);
}
if (count < 0)
{
throw new ArgumentOutOfRangeException(SR.TableCollectionCountNeedNonNegNum);
}
if (Size - index < count)
{
throw new ArgumentException(SR.TableCollectionRangeOutOfRange);
}
if (count > 0)
{
for (int i = index + count - 1; i >= index; --i)
{
Debug.Assert(BelongsToOwner(Items[i]));
PrivateDisconnectChild(Items[i]);
}
Size -= count;
for (int i = index; i < Size; ++i)
{View on GitHub (pinned to 81131a70a4)