{"record":{"id":"b4fdfad83935f376","repo":"dotnet/wpf","slug":"sr-collection-copyto-indexgreaterthanorequaltoarraylength","errorCode":null,"errorMessage":"SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength","messagePattern":"SR\\.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/PartialArray.cs","lineNumber":137,"sourceCode":"        }\n\n        public void CopyTo(T[] array, int arrayIndex)\n        {\n            // parameter validations\n            ArgumentNullException.ThrowIfNull(array);\n\n            if (array.Rank != 1)\n            {\n                throw new ArgumentException(\n                    SR.Collection_CopyTo_ArrayCannotBeMultidimensional, \n                    nameof(array));                \n            }\n\n            ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);\n\n            if (arrayIndex >= array.Length)\n            {\n                throw new ArgumentException(\n                    SR.Format(\n                        SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, \n                        \"arrayIndex\", \n                        \"array\"),\n                        nameof(arrayIndex));\n            }\n\n            if ((array.Length - Count - arrayIndex) < 0)\n            {\n                throw new ArgumentException(\n                    SR.Format(\n                        SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength,\n                        \"arrayIndex\",\n                        \"array\"));\n            }           \n            \n\n            // do the copying here","sourceCodeStart":119,"sourceCodeEnd":155,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/PartialArray.cs#L119-L155","documentation":"PartialArray<T>.CopyTo throws ArgumentException wrapping Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength when arrayIndex >= destination array.Length. The starting offset must point inside the destination array.","triggerScenarios":"Calling CopyTo(array, arrayIndex) where arrayIndex equals or exceeds array.Length, e.g. CopyTo(arr, arr.Length) or a stale offset after the destination was reallocated smaller.","commonSituations":"Cursor-based copy loops that increment arrayIndex without rechecking the destination length, or reuse of an offset computed for a different, larger array.","solutions":["Validate arrayIndex < array.Length before calling CopyTo.","Recompute the offset from the current destination array each call.","Ensure the destination array is allocated with length >= Count + arrayIndex."],"exampleFix":"// before\npartialArray.CopyTo(buffer, buffer.Length);\n// after\nif (buffer.Length > 0 && partialArray.Count <= buffer.Length)\n    partialArray.CopyTo(buffer, 0);","handlingStrategy":"validation","validationCode":"if (arrayIndex < 0 || arrayIndex >= array.Length) throw new ArgumentOutOfRangeException(nameof(arrayIndex));","typeGuard":"bool IsValidCopyOffset(Array dest, int arrayIndex) => arrayIndex >= 0 && arrayIndex < dest.Length;","tryCatchPattern":"try { coll.CopyTo(dest, arrayIndex); }\ncatch (ArgumentException e) when (e.ParamName == \"arrayIndex\") { /* fix offset and retry */ }","preventionTips":["Assert arrayIndex is within [0, array.Length-1] before CopyTo.","Recompute offsets after any destination reallocation.","Reserve at least one slot: destination length must exceed arrayIndex."],"tags":["wpf","collection","copyto","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-22T01:17:13.364Z"}