{"record":{"id":"08e7d4737daa0cdd","repo":"dotnet/wpf","slug":"sr-collection-badrank-vectorcollection","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/VectorCollection.cs","lineNumber":367,"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":349,"sourceCodeEnd":385,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/VectorCollection.cs#L349-L385","documentation":"VectorCollection.CopyTo(Array array, int index) requires the destination to be a single-dimensional Array with enough room starting at index (it also validates index >= 0 and index <= array.Length - Count beforehand). If array.Rank != 1 — i.e., a multidimensional array was supplied — it throws ArgumentException(SR.Collection_BadRank), mirroring BCL collection behavior.","triggerScenarios":"Calling ((ICollection)vectorCollection).CopyTo(multiDimArray, i) with a 2D (or higher-rank) array; also triggered if index is negative or larger than array.Length - Count via the preceding ArgumentOutOfRangeException checks.","commonSituations":"Generic copy-to-array helper code written against Array that happens to allocate rectangular arrays; porting code that used a 2D grid as scratch storage; interop code reusing buffers across calls.","solutions":["Allocate a one-dimensional Vector[] (or Array) with length >= index + collection.Count and pass that to CopyTo","Add a guard (array.Rank == 1) before calling CopyTo and handle multidimensional destinations with a manual loop","If a 2D layout is required, copy into a 1D array first and then reshape manually"],"exampleFix":"// before\nvar dest = new Vector[rows, cols];\ncollection.CopyTo(dest, 0);\n// after\nvar dest = new Vector[collection.Count];\ncollection.CopyTo(dest, 0);","handlingStrategy":"validation","validationCode":"if (dest == null) throw new ArgumentNullException(nameof(dest));\nif (dest.Rank != 1) throw new ArgumentException(\"Destination array must be one-dimensional.\", nameof(dest));\nif (index < 0 || index > dest.Length - vectorCollection.Count) throw new ArgumentOutOfRangeException(nameof(index));","typeGuard":"static bool CanCopyTo(VectorCollection c, Array a, int i) => a != null && a.Rank == 1 && i >= 0 && i <= a.Length - c.Count;","tryCatchPattern":"try { collection.CopyTo(dest, index); }\ncatch (ArgumentException ex) { /* dest was multidimensional (or wrong bounds); allocate a 1D array and retry */ }","preventionTips":["Always allocate flat (rank-1) arrays as copy destinations","Validate index + Count against array length before CopyTo","Avoid reusing multidimensional buffers as generic copy targets"],"tags":["wpf","collections","copyto","argument-exception"],"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"}