{"record":{"id":"17d9e1a723809c50","repo":"dotnet/wpf","slug":"cannot-pass-multidimensional-array-to-the-copyto-method-on-a","errorCode":null,"errorMessage":"Cannot pass multidimensional array to the CopyTo method on a collection.","messagePattern":"Cannot pass multidimensional array to the CopyTo method on a collection\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/PartialArray.cs","lineNumber":128,"sourceCode":"\n        #region ICollection<T> Members\n\n        public int Count\n        {\n            get\n            {\n                return _count;\n            }\n        }\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            {","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/PartialArray.cs#L110-L146","documentation":"PartialArray<T>.CopyTo validates the destination array and throws ArgumentException when the array's Rank is not 1, i.e. a multidimensional array (e.g. T[,]) was passed. ICollection<T>.CopyTo only supports single-dimensional arrays.","triggerScenarios":"Calling CopyTo with a 2D+ array such as new T[10,10], whether directly or via ICollection<T>.CopyTo/serialization code.","commonSituations":"Copying collection contents into a grid/matrix-shaped buffer, or reflection-based copy routines that pass through whatever array was at hand.","solutions":["Pass a single-dimensional array (T[]) as the destination.","If a 2D layout is needed, copy to a flat T[] first and map indices manually.","Add a Rank check before calling CopyTo in generic helper code."],"exampleFix":"// before\nvar grid = new T[n, m];\npartialArray.CopyTo(grid, 0);\n// after\nvar flat = new T[partialArray.Count];\npartialArray.CopyTo(flat, 0);","handlingStrategy":"validation","validationCode":"if (array == null) throw new ArgumentNullException(nameof(array));\nif (array.Rank != 1) throw new ArgumentException(\"Single-dimensional array required\", nameof(array));","typeGuard":"bool IsSingleDimensionalArray(Array a) => a.Rank == 1;","tryCatchPattern":"try { coll.CopyTo(dest, 0); }\ncatch (ArgumentException) { dest = new T[coll.Count]; coll.CopyTo(dest, 0); }","preventionTips":["Only pass T[] (Rank 1) arrays to CopyTo.","Add a Rank check in generic copy helpers.","Flatten multidimensional buffers before collection copies."],"tags":["wpf","collection","copyto","argument"],"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"}