{"record":{"id":"8d349c22fc2d145a","repo":"dotnet/wpf","slug":"sr-format-sr-collection-copyto-arraycannotbemultidimensional","errorCode":null,"errorMessage":"SR.Format(SR.Collection_CopyTo_ArrayCannotBeMultidimensional)","messagePattern":"SR\\.Format\\(SR\\.Collection_CopyTo_ArrayCannotBeMultidimensional\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Localizer/BamlLocalizationDictionary.cs","lineNumber":358,"sourceCode":"            get\n            {\n                ArgumentNullException.ThrowIfNull(key);\n                return ((IDictionary)_dictionary)[key];\n            }\n            set\n            {\n                ArgumentNullException.ThrowIfNull(key);\n                ((IDictionary)_dictionary)[key] = value;\n            }\n        }\n\n        IDictionaryEnumerator IDictionary.GetEnumerator() => GetEnumerator();\n\n        void ICollection.CopyTo(Array array, int index)\n        {\n            if (array != null && array.Rank != 1)\n            {\n                throw new ArgumentException(SR.Format(SR.Collection_CopyTo_ArrayCannotBeMultidimensional), nameof(array));\n            }\n\n            CopyTo(array as DictionaryEntry[], index);\n        }\n\n        int ICollection.Count\n        {\n            get => Count;\n        }\n\n        object ICollection.SyncRoot\n        {\n            get => ((IDictionary)_dictionary).SyncRoot;\n        }\n\n        bool ICollection.IsSynchronized\n        {\n            get => ((IDictionary)_dictionary).IsSynchronized;","sourceCodeStart":340,"sourceCodeEnd":376,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Localizer/BamlLocalizationDictionary.cs#L340-L376","documentation":"The explicit ICollection.CopyTo(Array, int) implementation only supports single-dimensional arrays because it writes DictionaryEntry elements sequentially. If a caller passes a non-null array with Rank != 1 (e.g. a 2D array), it throws ArgumentException with Collection_CopyTo_ArrayCannotBeMultidimensional. The typed CopyTo(DictionaryEntry[], int) is then used for the actual copy.","triggerScenarios":"Calling ((ICollection)dict).CopyTo with a multidimensional array such as new DictionaryEntry[2,2] or casting an object[,] destination; a null array is forwarded to the typed CopyTo and raises ArgumentNullException instead.","commonSituations":"Interoperating with legacy .NET 1.x collection APIs that use Array, callers reusing an existing multidimensional buffer for a copy, or generic copy helpers that accept Array and assume any rank works.","solutions":["Pass a single-dimensional DictionaryEntry[] (or Array castable to it) sized Count","Convert multidimensional buffers to 1D arrays and copy row-wise yourself","Check array.Rank == 1 before invoking the interface member","Use the typed CopyTo(DictionaryEntry[], int) directly to get clearer errors"],"exampleFix":"// before\nvar grid = new DictionaryEntry[2, 2];\n((ICollection)dict).CopyTo(grid, 0); // Rank != 1\n// after\nvar flat = new DictionaryEntry[dict.Count];\n((ICollection)dict).CopyTo(flat, 0);","handlingStrategy":"validation","validationCode":"if (array != null && array.Rank != 1)\n    throw new ArgumentException(\"ICollection.CopyTo requires a single-dimensional array.\", nameof(array));","typeGuard":"bool IsSingleDimensional(System.Array? array) => array == null || array.Rank == 1;","tryCatchPattern":"try\n{\n    ((ICollection)dict).CopyTo(array, index);\n}\ncatch (ArgumentException ex)\n{\n    // ex.ParamName == \"array\": switch to a 1D DictionaryEntry[] and retry\n}","preventionTips":["Use typed CopyTo(DictionaryEntry[], int) instead of the ICollection interface member","Flatten multidimensional buffers to 1D before copying collection contents","Check array.Rank when writing generic copy helpers over System.Array","Prefer List<T>/IEnumerable over Array-typed APIs in new code"],"tags":["wpf","argument-exception","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"}