{"record":{"id":"d8e839ffffcef0d3","repo":"dotnet/wpf","slug":"sr-collection-copyto-arraycannotbemultidimensional","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/CharacterMetricsDictionary.cs","lineNumber":196,"sourceCode":"                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            SC.DictionaryEntry[] typedArray = array as SC.DictionaryEntry[];\n            if (typedArray != null)\n            {\n                // it's an array of the exact type\n                foreach (KeyValuePair<int, CharacterMetrics> item in this)\n                {\n                    typedArray[index++] = new SC.DictionaryEntry(item.Key, item.Value);\n                }\n            }\n            else\n            {\n                // it's an array of some other type, e.g., object[]; make sure it's one dimensional\n                if (array.Rank != 1)\n                    throw new ArgumentException(SR.Collection_CopyTo_ArrayCannotBeMultidimensional);\n\n                // make sure the element type is compatible\n                Type elementType = array.GetType().GetElementType();\n                if (!elementType.IsAssignableFrom(typeof(SC.DictionaryEntry)))\n                    throw new ArgumentException(SR.Format(SR.CannotConvertType, typeof(SC.DictionaryEntry), elementType));\n\n                foreach (KeyValuePair<int, CharacterMetrics> item in this)\n                {\n                    array.SetValue(new SC.DictionaryEntry(item.Key, item.Value), index++);\n                }\n            }\n        }\n\n        #endregion\n\n        #region IDictionary members\n\n        /// <summary>","sourceCodeStart":178,"sourceCodeEnd":214,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs#L178-L214","documentation":"The non-generic ICollection.CopyTo of CharacterMetricsDictionary only supports one-dimensional arrays when the target is not exactly DictionaryEntry[]. If array.Rank != 1 (a multidimensional array such as object[,]), an ArgumentException with Collection_CopyTo_ArrayCannotBeMultidimensional is thrown.","triggerScenarios":"Passing a 2D+ array (e.g. object[,], DictionaryEntry[,]) to ((ICollection)CharacterMetricsDictionary).CopyTo.","commonSituations":"Legacy code written against rectangular array buffers; generic helper methods that accept Array and accidentally receive multidimensional instances; binding/serialization frameworks using 2D scratch arrays.","solutions":["Use a one-dimensional array (e.g. DictionaryEntry[] or object[]).","Flatten or slice the multidimensional array into a 1D view before copying.","If you need positional storage, copy into a 1D array and index it manually into the 2D structure."],"exampleFix":"// before\nvar grid = new object[dict.Count, 2];\n((ICollection)dict).CopyTo(grid, 0);\n// after\nvar flat = new object[((ICollection)dict).Count];\n((ICollection)dict).CopyTo(flat, 0);","handlingStrategy":"validation","validationCode":"if (array.Rank != 1)\n    throw new ArgumentException(\"CopyTo requires a one-dimensional array.\");","typeGuard":null,"tryCatchPattern":"try { ((ICollection)dict).CopyTo(array, index); }\ncatch (ArgumentException) { /* switch to a 1D array */ }","preventionTips":["Only pass 1D arrays to non-generic CopyTo.","Flatten multidimensional buffers first.","Prefer the strongly typed KeyValuePair[] overload to avoid these checks."],"tags":["wpf","icollection","copyto","multidimensional-array"],"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"}