{"record":{"id":"5cfeb0fc2a74f0a8","repo":"dotnet/wpf","slug":"sr-collection-copyto-arraycannotbemultidimensional-5cfeb0","errorCode":null,"errorMessage":"SR.Collection_CopyTo_ArrayCannotBeMultidimensional","messagePattern":"SR\\.Collection_CopyTo_ArrayCannotBeMultidimensional","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyMapCollection.cs","lineNumber":91,"sourceCode":"        {\n            ArgumentNullException.ThrowIfNull(array);\n\n            if (index >= array.Length)\n                throw new ArgumentException(SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, \"index\", \"array\"));\n\n            if (_count > array.Length - index)\n                throw new ArgumentException(SR.Format(SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength, index, \"array\"));\n\n            if (_count != 0)\n                Array.Copy(_items, 0, array, index, _count);\n        }\n\n        void SC.ICollection.CopyTo(Array array, int index)\n        {\n            ArgumentNullException.ThrowIfNull(array);\n\n            if (array.Rank != 1)\n                throw new ArgumentException(SR.Collection_CopyTo_ArrayCannotBeMultidimensional);\n\n            Type elementType = array.GetType().GetElementType();\n            if (!elementType.IsAssignableFrom(typeof(FamilyTypeface)))\n                throw new ArgumentException(SR.Format(SR.CannotConvertType, typeof(FamilyTypeface), elementType));\n\n            if (index >= array.Length)\n                throw new ArgumentException(SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, \"index\", \"array\"));\n\n            if (_count > array.Length - index)\n                throw new ArgumentException(SR.Format(SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength, index, \"array\"));\n\n            if (_count != 0)\n                Array.Copy(_items, 0, array, index, _count);\n        }\n\n        bool SC.ICollection.IsSynchronized\n        {\n            get { return false; }","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyMapCollection.cs#L73-L109","documentation":"The explicit ICollection.CopyTo(Array, int) implementation of FamilyMapCollection rejects multidimensional arrays: array.Rank != 1 throws ArgumentException with Collection_CopyTo_ArrayCannotBeMultidimensional, since it performs a flat Array.Copy.","triggerScenarios":"Calling ((ICollection)familyMapCollection).CopyTo with a 2D (or higher-rank) Array instance as the destination.","commonSituations":"Generic copy helpers that take Array and allocate new int[rows, cols]-style buffers, or code converting from multidimensional grid storage to the collection.","solutions":["Pass a one-dimensional array (or a jagged array's inner 1D arrays) as the destination.","Flatten multidimensional buffers before copying, or use OfType<FontFamilyMap>().ToArray() on the collection.","Add a Rank check in helper code before dispatching to ICollection.CopyTo."],"exampleFix":"// before\nvar dest = new FontFamilyMap[10, 10];\n((ICollection)familyMaps).CopyTo(dest, 0);\n// after\nvar dest = new FontFamilyMap[familyMaps.Count];\n((ICollection)familyMaps).CopyTo(dest, 0);","handlingStrategy":"type-guard","validationCode":"if (array.Rank != 1) array = new FontFamilyMap[col.Count];","typeGuard":"static bool IsFlatAssignableArray(Array a) => a.Rank == 1 && a.GetType().GetElementType().IsAssignableFrom(typeof(FontFamilyMap));","tryCatchPattern":"try { ((ICollection)col).CopyTo(array, index); }\ncatch (ArgumentException) { /* allocate 1D array and retry */ }","preventionTips":["Never pass multidimensional arrays to ICollection.CopyTo","Allocate 1D buffers for collection copies"],"tags":["wpf","fonts","collections","arrays"],"backgroundTag":"type-mismatch","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"}