{"record":{"id":"1591856d373edfda","repo":"dotnet/wpf","slug":"sr-collection-copyto-arraycannotbemultidimensional-159185","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/FamilyTypefaceCollection.cs","lineNumber":369,"sourceCode":"        }\n\n        private FamilyTypeface ConvertValue(object obj)\n        {\n            ArgumentNullException.ThrowIfNull(obj);\n\n            FamilyTypeface familyTypeface = obj as FamilyTypeface;\n            if (familyTypeface == null)\n                throw new ArgumentException(SR.Format(SR.CannotConvertType, obj.GetType(), typeof(FamilyTypeface)));\n\n            return familyTypeface;\n        }\n\n        private void CopyItems(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            {\n                InitializeItemsFromInnerList();\n                Array.Copy(_items, 0, array, index, _count);\n            }\n        }\n","sourceCodeStart":351,"sourceCodeEnd":387,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyTypefaceCollection.cs#L351-L387","documentation":"FamilyTypefaceCollection.CopyTo supports only single-dimensional arrays. CopyItems throws ArgumentException with Collection_CopyTo_ArrayCannotBeMultidimensional when the destination array's Rank is not 1, matching ICollection.CopyTo conventions.","triggerScenarios":"Calling CopyTo with a multidimensional array (e.g. FamilyTypeface[,] or Array.CreateInstance with rank 2) as the destination.","commonSituations":"Generic copying utilities that allocate rectangular arrays for collections, or interop code that assumes multidimensional arrays are supported.","solutions":["Copy into a one-dimensional FamilyTypeface[] instead.","Flatten the multidimensional array copy by iterating with nested loops and adding items individually.","Use LINQ: FamilyTypeface[] arr = collection.ToArray();"],"exampleFix":"// before\nvar grid = new FamilyTypeface[2, 5];\ncollection.CopyTo(grid, 0); // ArgumentException\n// after\nvar flat = new FamilyTypeface[collection.Count];\ncollection.CopyTo(flat, 0);","handlingStrategy":"validation","validationCode":"if (array == null) throw new ArgumentNullException(nameof(array));\nif (array.Rank != 1) throw new ArgumentException(\"CopyTo requires a single-dimensional array.\");","typeGuard":null,"tryCatchPattern":"try { collection.CopyTo(array, 0); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Multidimensional\")) { array = new FamilyTypeface[collection.Count]; collection.CopyTo(array, 0); }","preventionTips":["Always use FamilyTypeface[] for CopyTo destinations.","Avoid rectangular arrays when interacting with ICollection.CopyTo.","Prefer ToArray()/ToList for snapshotting."],"tags":["wpf","copyto","arrays","invalid-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-22T01:17:13.364Z"}