{"record":{"id":"4720b91aa63957ec","repo":"dotnet/wpf","slug":"sr-collection-badrank-visualcollection","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/VisualCollection.cs","lineNumber":174,"sourceCode":"            {\n                VerifyAPIReadOnly();\n\n                return this;\n            }\n        }\n\n        /// <summary>\n        /// Copies the Visual collection to the specified array starting at the specified index.\n        /// </summary>\n        public void CopyTo(Array array, int index)\n        {\n            VerifyAPIReadOnly();\n\n            ArgumentNullException.ThrowIfNull(array);\n\n            if (array.Rank != 1)\n            {\n                throw new ArgumentException(SR.Collection_BadRank);\n            }\n\n            ArgumentOutOfRangeException.ThrowIfNegative(index);\n            ArgumentOutOfRangeException.ThrowIfGreaterThan(index, array.Length - _size);\n\n            // System.Array does not have a CopyTo method that takes a count. Therefore\n            // the loop is programmed here out.\n            for (int i=0; i < _size; i++)\n            {\n                array.SetValue(_items[i], i+index);\n            }\n}\n\n        /// <summary>\n        /// Copies the Visual collection to the specified array starting at the specified index.\n        /// </summary>\n        public void CopyTo(Visual[] array, int index)\n        {","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/VisualCollection.cs#L156-L192","documentation":"ArgumentException thrown by VisualCollection.CopyTo when the target array's Rank is not 1 — the copy helper only supports single-dimensional arrays, so a multidimensional destination array is rejected even if it is large enough.","triggerScenarios":"Calling CopyTo with a 2D array, e.g. visuals.CopyTo(new Visual[2,5], 0), or any array created with more than one dimension.","commonSituations":"Implementing generic serialization/dumping code that passes a rect array or matrix array to CopyTo; misunderstanding that 'array' overloads accept only flat arrays.","solutions":["Pass a one-dimensional Visual[] (or Array) sized at least count + index.","If data is inherently 2D, copy into a flat array first and reshape yourself.","Validate array.Rank == 1 before calling to fail fast with a clearer message."],"exampleFix":"// before\nvar grid = new Visual[rows, cols];\nvisualCollection.CopyTo(grid, 0); // ArgumentException\n// after\nvar flat = new Visual[visualCollection.Count];\nvisualCollection.CopyTo(flat, 0);","handlingStrategy":"validation","validationCode":"if (targetArray.Rank != 1 || targetArray.Length - index < visualCollection.Count)\n    throw new ArgumentException(\"CopyTo requires a 1-D array with enough room\");\nvisualCollection.CopyTo(targetArray, index);","typeGuard":"static bool CanCopyTo(Array a, int index, int count) => a.Rank == 1 && a.Length - index >= count;","tryCatchPattern":"try { collection.CopyTo(arr, 0); }\ncatch (ArgumentException) { /* wrong array rank or size */ }","preventionTips":["Always pass flat, zero-based arrays to ICollection.CopyTo.","Size the destination as count + index before copying.","For 2D data, copy to a flat buffer and reshape manually."],"tags":["wpf","visual-collection","copyto","array-rank","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"}