{"record":{"id":"ca0eba7afe58d420","repo":"dotnet/wpf","slug":"sr-collection-badrank-collectionhelper","errorCode":null,"errorMessage":"SR.Collection_BadRank","messagePattern":"SR\\.Collection_BadRank","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WpfGfx/codegen/mcg/helpers/CollectionHelper.cs","lineNumber":369,"sourceCode":"                    {\n                        [[WriteCopyToCommon(resource)]]\n\n                        _collection.CopyTo(array, index);\n                    }\n                [[/inline]];\n        }\n\n        public static string WriteCopyToArray(McgResource resource)\n        {\n            return\n                [[inline]]\n                    void ICollection.CopyTo(Array array, int index)\n                    {\n                        [[WriteCopyToCommon(resource)]]\n\n                        if (array.Rank != 1)\n                        {\n                            throw new ArgumentException(SR.Collection_BadRank);\n                        }\n\n                        // Elsewhere in the collection we throw an AE when the type is\n                        // bad so we do it here as well to be consistent\n                        try\n                        {\n                            int count = _collection.Count;\n                            for (int i = 0; i < count; i++)\n                            {\n                                array.SetValue(_collection[i], index + i);\n                            }\n                        }\n                        catch (InvalidCastException e)\n                        {\n                            throw new ArgumentException(SR.Format(SR.Collection_BadDestArray, this.GetType().Name), e);\n                        }\n                    }\n                [[/inline]];","sourceCodeStart":351,"sourceCodeEnd":387,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WpfGfx/codegen/mcg/helpers/CollectionHelper.cs#L351-L387","documentation":"This SR resource backs an ArgumentException thrown by the generated ICollection.CopyTo(Array, int) implementation (CollectionHelper.cs, Collection_SetValue) when the destination array has Rank != 1. CopyTo only supports single-dimensional destination arrays.","triggerScenarios":"Calling CopyTo on a generated collection passing a multidimensional array (e.g. new object[2,2]) as the destination.","commonSituations":"Reusing a rectangular/jagged array allocated elsewhere; generic code that accepts Array and builds arrays of arbitrary rank; legacy interop code using 2D buffers.","solutions":["Pass a single-dimensional array of sufficient length: new T[count]","Validate array.Rank == 1 before calling CopyTo","If you need 2D storage, copy into a 1D array and repack manually","Catch ArgumentException and give callers a clear message about array rank"],"exampleFix":"// before\ncollection.CopyTo(new object[rows, cols], 0); // BadRank\n// after\nvar flat = new object[collection.Count];\ncollection.CopyTo(flat, 0);","handlingStrategy":"validation","validationCode":"if (array == null) throw new ArgumentNullException(nameof(array));\nif (array.Rank != 1) throw new ArgumentException(\"destination array must be 1-D\", nameof(array));\nif (array.Length - index < collection.Count) throw new ArgumentException(\"array too small\");","typeGuard":null,"tryCatchPattern":"try { collection.CopyTo(array, index); }\ncatch (ArgumentException ex) { throw new ArgumentException(\"CopyTo requires a 1-D array\", ex); }","preventionTips":["Always allocate new T[Count] for CopyTo destinations","Never reuse multidimensional buffers as CopyTo targets"],"tags":["wpf","collection","copyto","array-rank"],"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"}