{"record":{"id":"83ceb938d8bb21b5","repo":"dotnet/wpf","slug":"collection-copyto-indexgreaterthanorequaltoarraylength","errorCode":null,"errorMessage":"Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength","messagePattern":"Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/TextFormatting/ThousandthOfEmRealDoubles.cs","lineNumber":178,"sourceCode":"        }\n\n        public void CopyTo(double[] 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":160,"sourceCodeEnd":196,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/TextFormatting/ThousandthOfEmRealDoubles.cs#L160-L196","documentation":"ThousandthOfEmRealDoubles.CopyTo throws ArgumentException(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength) when arrayIndex >= array.Length. There is no room at the given start index, so the standard collection contract requires this error.","triggerScenarios":"Calling CopyTo with an arrayIndex equal to or larger than the destination array length, e.g. CopyTo(new double[10], 10) or CopyTo(new double[10], 50).","commonSituations":"Off-by-one indexing (using array.Length instead of a computed free position); reusing an index computed for a larger buffer; copying into a shrunk buffer.","solutions":["Pass arrayIndex strictly less than array.Length (start at 0 for a fresh buffer)","Validate with: if (arrayIndex >= array.Length) fix the index before calling","Track buffer occupancy explicitly so the next CopyTo offset is computed correctly"],"exampleFix":"// before\nint offset = itemsCopied; // e.g. equals dest.Length\ncollection.CopyTo(dest, offset);\n// after\nif (offset < dest.Length) collection.CopyTo(dest, offset);","handlingStrategy":"validation","validationCode":"if (index >= dest.Length)\n    throw new ArgumentException(\"index must be < dest.Length\");\ncollection.CopyTo(dest, index);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Compute copy offsets as remaining-space positions, not as counts copied","Use index 0 for fresh buffers","Add debug assertions that index < dest.Length before CopyTo"],"tags":["wpf","collections","copyto","argument-exception"],"backgroundTag":"invalid-argument-value","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"}