dotnet/wpf · error · ArgumentException
SR.Collection_BadRank
Error message
SR.Collection_BadRank
What it means
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.
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
Example fix
// before var dest = new Vector[rows, cols]; collection.CopyTo(dest, 0); // after var dest = new Vector[collection.Count]; collection.CopyTo(dest, 0);
Defensive patterns
Strategy: validation
Validate before calling
if (dest == null) throw new ArgumentNullException(nameof(dest));
if (dest.Rank != 1) throw new ArgumentException("Destination array must be one-dimensional.", nameof(dest));
if (index < 0 || index > dest.Length - vectorCollection.Count) throw new ArgumentOutOfRangeException(nameof(index)); Type guard
static bool CanCopyTo(VectorCollection c, Array a, int i) => a != null && a.Rank == 1 && i >= 0 && i <= a.Length - c.Count;
Try / catch
try { collection.CopyTo(dest, index); }
catch (ArgumentException ex) { /* dest was multidimensional (or wrong bounds); allocate a 1D array and retry */ } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Collection_CopyTo_ArrayCannotBeMultidimensional
- Collection_CopyTo_ArrayCannotBeMultidimensional
- Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength
- Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength
- Collection_CopyTo_NumberOfElementsExceedsArrayLength
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/08e7d4737daa0cdd.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/VectorCollection.cs:367
#endregion
#region ICollection
void ICollection.CopyTo(Array array, int index)
{
ReadPreamble();
ArgumentNullException.ThrowIfNull(array);
// This will not throw in the case that we are copying
// from an empty collection. This is consistent with the
// BCL Collection implementations. (Windows 1587365)
ArgumentOutOfRangeException.ThrowIfNegative(index);
ArgumentOutOfRangeException.ThrowIfGreaterThan(index, array.Length - _collection.Count);
if (array.Rank != 1)
{
throw new ArgumentException(SR.Collection_BadRank);
}
// Elsewhere in the collection we throw an AE when the type is
// bad so we do it here as well to be consistent
try
{
int count = _collection.Count;
for (int i = 0; i < count; i++)
{
array.SetValue(_collection[i], index + i);
}
}
catch (InvalidCastException e)
{
throw new ArgumentException(SR.Format(SR.Collection_BadDestArray, this.GetType().Name), e);
}
}
View on GitHub (pinned to 81131a70a4)