{"record":{"id":"5cebe987ef3b5cb4","repo":"dotnet/wpf","slug":"collection-copyto-numberofelementsexceedsarraylength","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/ThousandthOfEmRealDoubles.cs","lineNumber":188,"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<double> GetEnumerator()\n        {\n            for (int i = 0; i < Count; i++)\n            {","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/TextFormatting/ThousandthOfEmRealDoubles.cs#L170-L206","documentation":"ThousandthOfEmRealDoubles.CopyTo throws ArgumentException(SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength) when (array.Length - Count - arrayIndex) < 0, i.e. the destination array lacks space for Count elements starting at arrayIndex. This is the standard ICollection.CopyTo capacity contract.","triggerScenarios":"Calling CopyTo with a destination array whose available space (Length - arrayIndex) is smaller than the collection's Count, e.g. CopyTo(new double[3], 0) on a collection with 5 items.","commonSituations":"Sizing the buffer from a stale Count (collection changed since sizing); forgetting to account for a non-zero arrayIndex offset; under-allocating by using array length of the source instead of Count.","solutions":["Allocate the destination with at least collection.Count + arrayIndex elements: new double[collection.Count]","Check dest.Length - arrayIndex >= collection.Count before calling","Copy to offset 0 with a correctly sized buffer to avoid double-counting the index"],"exampleFix":"// before\nvar dest = new double[collection.Count / 2];\ncollection.CopyTo(dest, 0);\n// after\nvar dest = new double[collection.Count];\ncollection.CopyTo(dest, 0);","handlingStrategy":"validation","validationCode":"if (dest.Length - index < collection.Count)\n    throw new ArgumentException(\"dest too small\");\ncollection.CopyTo(dest, index);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Size buffers as new T[collection.Count + index]","Re-read Count immediately before CopyTo if the collection may change","Account for the offset when sizing, not only when indexing"],"tags":["wpf","collections","copyto","argument-exception","buffer-size"],"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-21T21:30:21.729Z"}