dotnet/wpf · error · ArgumentOutOfRangeException
SR.TableCollectionCountNeedNonNegNum
Error message
SR.TableCollectionCountNeedNonNegNum
What it means
TableTextElementCollectionInternal<T>.RemoveRange rejects a negative count with ArgumentOutOfRangeException SR.TableCollectionCountNeedNonNegNum. The check happens after the index check, so index must already be in range; only a negative count triggers this particular error.
Solutions
- Validate/clamp the count first: count = Math.Max(0, count), and skip the call when it is zero.
- Fix the subtraction producing the count; compute it as Math.Max(0, end - start).
- For inverted ranges, normalize so start <= end before computing the count.
- Add a debug assertion on count >= 0 at the call site to catch regressions early.
Example fix
// before collection.RemoveRange(start, end - start); // throws when end < start // after int count = Math.Max(0, end - start); if (count > 0) collection.RemoveRange(start, count);
Defensive patterns
Strategy: validation
Validate before calling
int count = Math.Max(0, end - start);
if (count > 0 && start >= 0 && start < collection.Count)
collection.RemoveRange(start, count); Type guard
static bool IsValidRange(int start, int end) => end >= start && start >= 0;
Try / catch
try { collection.RemoveRange(index, count); }
count < 0 check: catch (ArgumentOutOfRangeException ex) when (count < 0) { count = 0; } Prevention
- Normalize ranges so start <= end before computing count
- Clamp every computed count with Math.Max(0, ...)
- Assert count >= 0 at call sites in debug builds
When it happens
Trigger: RemoveRange(index, count) with count < 0 — typically from a subtraction like end - start where end < start, or an uninitialized/default variable.
Common situations: Range bookkeeping where the end was captured before the start was updated, selection-range math on inverted selections, and refactored code that swapped parameter order.
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/dc176c751a2d83d2.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/TableTextElementCollectionInternal.cs:225
/// <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]));
Remove(Items[i]);
}
}
}
/// <summary>
/// Sets the specified TItem at the specified index;View on GitHub (pinned to 81131a70a4)