{"record":{"id":"3b000d29f05eb8ee","repo":"dotnet/wpf","slug":"sr-collection-badrank-drawingcollection","errorCode":null,"errorMessage":"SR.Collection_BadRank","messagePattern":"SR\\.Collection_BadRank","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/DrawingCollection.cs","lineNumber":414,"sourceCode":"        #endregion\n\n        #region ICollection\n\n        void ICollection.CopyTo(Array array, int index)\n        {\n            ReadPreamble();\n\n            ArgumentNullException.ThrowIfNull(array);\n\n            // This will not throw in the case that we are copying\n            // from an empty collection.  This is consistent with the\n            // BCL Collection implementations. (Windows 1587365)\n            ArgumentOutOfRangeException.ThrowIfNegative(index);\n            ArgumentOutOfRangeException.ThrowIfGreaterThan(index, array.Length - _collection.Count);\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","sourceCodeStart":396,"sourceCodeEnd":432,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/DrawingCollection.cs#L396-L432","documentation":"DrawingCollection's ICollection.CopyTo validates the destination Array's rank and throws ArgumentException with SR.Collection_BadRank when array.Rank != 1 (a multidimensional array). The BCL collection contract only supports copying into single-dimensional, zero-based arrays.","triggerScenarios":"Calling ((ICollection)drawingCollection).CopyTo(array, index) where array is a 2D (or higher-rank) Array, e.g. new Drawing[n, m].","commonSituations":"Reusing a multidimensional scratch buffer as a copy destination; generic array-copy helpers that accept Array and pass through whatever rank the caller supplies; legacy code assuming rank flexibility.","solutions":["Pass a single-dimensional Drawing[] (or compatible element-type 1D array) to CopyTo.","Copy manually into the multidimensional array with a foreach loop and explicit index math.","Declare the destination buffer as a flat 1D array and compute offsets yourself."],"exampleFix":"// before\nvar dest = new Drawing[rows, cols];\n((ICollection)collection).CopyTo(dest, 0); // ArgumentException: Collection_BadRank\n// after\nvar dest = new Drawing[collection.Count];\ncollection.CopyTo(dest, 0);","handlingStrategy":"validation","validationCode":"if (array.Rank != 1)\n    throw new ArgumentException(\"CopyTo requires a single-dimensional array.\", nameof(array));\n((ICollection)coll).CopyTo(array, index);","typeGuard":"static bool IsSingleDimension(Array a) => a != null && a.Rank == 1;","tryCatchPattern":"try { ((ICollection)coll).CopyTo(array, index); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Collection_BadRank\"))\n{ /* allocate a 1D Drawing[] and copy manually */ }","preventionTips":["Declare CopyTo destination buffers as 1D arrays.","Add rank assertions in generic array-copy helpers.","Copy into multidimensional arrays manually with explicit loops when needed."],"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"}