{"record":{"id":"4d154064aad6d473","repo":"dotnet/wpf","slug":"collection-copyto-numberofelementsexceedsarraylength-4d1540","errorCode":null,"errorMessage":"Collection_CopyTo_NumberOfElementsExceedsArrayLength","messagePattern":"Collection_CopyTo_NumberOfElementsExceedsArrayLength","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/TextFormatting/ThousandthOfEmRealPoints.cs","lineNumber":133,"sourceCode":"                    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\n            for (int i = 0; i < Count; i++)\n            {\n                array[arrayIndex + i] = this[i];\n            }\n        }\n\n        public IEnumerator<Point> GetEnumerator()\n        {\n            for (int i = 0; i < Count; i++)\n            {","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/TextFormatting/ThousandthOfEmRealPoints.cs#L115-L151","documentation":"Thrown by ThousandthOfEmRealPoints.CopyTo when the destination array does not have room for all collection elements starting at arrayIndex. It is a standard ArgumentException guarding the ICollection.CopyTo contract, analogous to copying a Point[] into a too-small target.","triggerScenarios":"Calling CopyTo(array, arrayIndex) where array.Length - Count - arrayIndex < 0, i.e. the remaining space after arrayIndex is smaller than the collection's Count, or arrayIndex itself is beyond the array bounds.","commonSituations":"Marshalling a glyph-run/advance-width Point collection into a pre-allocated buffer sized for a different text run (font size or character count changed), or copying with an offset index into an array reused from a previous, larger run.","solutions":["Size the destination array to at least collection.Count + arrayIndex before copying (e.g. Array.Resize or allocate new Point[count]).","Check array.Length - arrayIndex >= collection.Count before calling CopyTo.","Verify arrayIndex is >= 0 and within array bounds; a stale offset is a frequent cause."],"exampleFix":"// before\nvar points = new Point[oldCount];\ncollection.CopyTo(points, 0);\n// after\nvar points = new Point[collection.Count];\ncollection.CopyTo(points, 0);","handlingStrategy":"validation","validationCode":"if (array == null) throw new ArgumentNullException(nameof(array));\nif (arrayIndex < 0 || arrayIndex > array.Length) throw new ArgumentOutOfRangeException(nameof(arrayIndex));\nif (array.Length - arrayIndex < collection.Count)\n    throw new ArgumentException(\"Destination array too small\", nameof(array));","typeGuard":null,"tryCatchPattern":"try { collection.CopyTo(arr, idx); }\ncatch (ArgumentException) { arr = new Point[collection.Count]; collection.CopyTo(arr, 0); }","preventionTips":["Always allocate destination arrays from collection.Count, not from a stale size.","Account for arrayIndex when sizing the buffer.","Prefer CopyTo with index 0 into a freshly sized array."],"tags":["argument-exception","copyto","array-bounds","wpf","text-formatting"],"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"}