dotnet/wpf · error · ArgumentOutOfRangeException
SR.ArgumentOutOfRange_NeedNonNegNum
Error message
SR.ArgumentOutOfRange_NeedNonNegNum
What it means
The destination index must be non-negative; the library throws ArgumentOutOfRangeException with SR.ArgumentOutOfRange_NeedNonNegNum when the index passed to CopyTo is negative. The index is the offset in the destination array where copying starts.
Solutions
- Clamp or validate index >= 0 before calling CopyTo
- Fix the offset computation that produced the negative value
- Use index 0 when copying to a freshly allocated array
Example fix
// before collection.CopyTo(target, offset - extra); // may be -1 // after collection.CopyTo(target, Math.Max(0, offset - extra));
Defensive patterns
Strategy: validation
Validate before calling
if (index < 0) throw new ArgumentOutOfRangeException(nameof(index), "Index must be non-negative.");
Type guard
bool IsValidIndex(int i) => i >= 0;
Try / catch
try { ((ICollection)collection).CopyTo(array, index); } catch (ArgumentOutOfRangeException) { /* clamp index or fix offset computation */ } Prevention
- Clamp computed offsets with Math.Max(0, x)
- Never use -1 as a sentinel that can reach CopyTo
- Unit-test copy helpers with boundary indices
When it happens
Trigger: Calling CopyTo(array, index) with a negative index (e.g., a computed offset that underflowed, or -1 used as a sentinel).
Common situations: Loop-computed offsets that went negative on empty collections; sentinel values leaked into the copy call.
Related errors
- arrayIndex
- SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArra…
- args
- args
- ArgumentOutOfRangeException(authenticationType)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/277d5bbb0949c37d.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Collections/ObjectModel/WeakReadOnlyCollection.cs:151
}
}
void ICollection.CopyTo(Array array, int index) {
ArgumentNullException.ThrowIfNull(array);
if (array.Rank != 1) {
//ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);
throw new ArgumentException(SR.Arg_RankMultiDimNotSupported);
}
if( array.GetLowerBound(0) != 0 ) {
//ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_NonZeroLowerBound);
throw new ArgumentException(SR.Arg_NonZeroLowerBound);
}
if (index < 0) {
//ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.arrayIndex, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_NeedNonNegNum);
}
if (array.Length - index < Count) {
//ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
throw new ArgumentException(SR.Arg_ArrayPlusOffTooSmall);
}
IList<T> dlist = CreateDereferencedList();
if (array is T[] items)
{
dlist.CopyTo(items, index);
}
else
{
//
// Catch the obvious case assignment will fail.
// We can found all possible problems by doing the check though.
// For example, if the element type of the Array is derived from T,View on GitHub (pinned to 81131a70a4)