{"record":{"id":"277d5bbb0949c37d","repo":"dotnet/wpf","slug":"sr-argumentoutofrange-neednonnegnum","errorCode":null,"errorMessage":"SR.ArgumentOutOfRange_NeedNonNegNum","messagePattern":"SR\\.ArgumentOutOfRange_NeedNonNegNum","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Collections/ObjectModel/WeakReadOnlyCollection.cs","lineNumber":151,"sourceCode":"            }\n        }\n\n        void ICollection.CopyTo(Array array, int index) {\n            ArgumentNullException.ThrowIfNull(array);\n\n            if (array.Rank != 1) {\n                //ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);\n                throw new ArgumentException(SR.Arg_RankMultiDimNotSupported);\n            }\n\n            if( array.GetLowerBound(0) != 0 ) {\n                //ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_NonZeroLowerBound);\n                throw new ArgumentException(SR.Arg_NonZeroLowerBound);\n            }\n\n            if (index < 0) {\n                //ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.arrayIndex, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);\n                throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_NeedNonNegNum);\n            }\n\n            if (array.Length - index < Count) {\n                //ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);\n                throw new ArgumentException(SR.Arg_ArrayPlusOffTooSmall);\n            }\n\n            IList<T> dlist = CreateDereferencedList();\n            if (array is T[] items)\n            {\n                dlist.CopyTo(items, index);\n            }\n            else\n            {\n                //\n                // Catch the obvious case assignment will fail.\n                // We can found all possible problems by doing the check though.\n                // For example, if the element type of the Array is derived from T,","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Collections/ObjectModel/WeakReadOnlyCollection.cs#L133-L169","documentation":"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.","triggerScenarios":"Calling CopyTo(array, index) with a negative index (e.g., a computed offset that underflowed, or -1 used as a sentinel).","commonSituations":"Loop-computed offsets that went negative on empty collections; sentinel values leaked into the copy call.","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"],"exampleFix":"// before\ncollection.CopyTo(target, offset - extra); // may be -1\n// after\ncollection.CopyTo(target, Math.Max(0, offset - extra));","handlingStrategy":"validation","validationCode":"if (index < 0) throw new ArgumentOutOfRangeException(nameof(index), \"Index must be non-negative.\");","typeGuard":"bool IsValidIndex(int i) => i >= 0;","tryCatchPattern":"try { ((ICollection)collection).CopyTo(array, index); } catch (ArgumentOutOfRangeException) { /* clamp index or fix offset computation */ }","preventionTips":["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"],"tags":["wpf","copyto","argument-out-of-range"],"backgroundTag":"argument-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"}