{"record":{"id":"3ade6f7cf53f2731","repo":"dotnet/wpf","slug":"sr-collection-copyto-numberofelementsexceedsarraylength","errorCode":null,"errorMessage":"SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength","messagePattern":"SR\\.Collection_CopyTo_NumberOfElementsExceedsArrayLength","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/PartialArray.cs","lineNumber":147,"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        #endregion\n\n        #region IEnumerable<T> Members\n","sourceCodeStart":129,"sourceCodeEnd":165,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/PartialArray.cs#L129-L165","documentation":"PartialArray<T>.CopyTo throws ArgumentException wrapping Collection_CopyTo_NumberOfElementsExceedsArrayLength when the destination array has insufficient room: (array.Length - Count - arrayIndex) < 0, meaning Count elements starting at arrayIndex do not fit.","triggerScenarios":"Calling CopyTo(array, arrayIndex) where array.Length < Count + arrayIndex — e.g. copying a full PartialArray into a smaller buffer or at a nonzero offset.","commonSituations":"Paging/batching code that reuses undersized scratch buffers, or assuming Count is smaller than it actually is.","solutions":["Allocate the destination with length >= arrayIndex + partialArray.Count.","Check (array.Length - arrayIndex) >= Count before calling CopyTo.","Copy only the portion that fits by adjusting ranges manually if a partial copy is intended."],"exampleFix":"// before\npartialArray.CopyTo(smallBuffer, offset);\n// after\nif (smallBuffer.Length - offset >= partialArray.Count)\n    partialArray.CopyTo(smallBuffer, offset);","handlingStrategy":"validation","validationCode":"if (array.Length - arrayIndex < coll.Count) array = new T[arrayIndex + coll.Count];","typeGuard":"bool HasRoomForCopy(Array dest, int arrayIndex, int count) => dest.Length - arrayIndex >= count;","tryCatchPattern":"try { coll.CopyTo(dest, arrayIndex); }\ncatch (ArgumentException) { dest = new T[arrayIndex + coll.Count]; coll.CopyTo(dest, arrayIndex); }","preventionTips":["Size destinations as arrayIndex + Count before copying.","Recheck buffer length whenever Count may have changed.","Prefer allocating fresh exact-size buffers in batching code."],"tags":["wpf","collection","copyto","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"}