dotnet/wpf · error · ArgumentException
SR.Collection_BadRank
Error message
SR.Collection_BadRank
What it means
PathSegmentCollection.CopyTo throws ArgumentException with SR.Collection_BadRank when the destination array is not one-dimensional (Rank != 1). CopyTo requires a single-dimensional Array because it writes elements by linear index.
Solutions
- Use a one-dimensional PathSegment[] as the destination.
- If you need 2D storage, copy into a flat 1D array first and then reshape manually.
- Check array.Rank == 1 before calling CopyTo and handle the multidimensional case separately.
Example fix
// before var grid = new PathSegment[2, 5]; collection.CopyTo(grid, 0); // after var flat = new PathSegment[collection.Count]; collection.CopyTo(flat, 0);
Defensive patterns
Strategy: validation
Validate before calling
if (array == null) throw new ArgumentNullException(nameof(array));
if (array.Rank != 1) throw new ArgumentException("Destination array must be one-dimensional", nameof(array));
collection.CopyTo(array, index); Type guard
bool IsFlatSegmentArray(Array a) => a is PathSegment[];
Try / catch
try
{
collection.CopyTo(array, 0);
}
catch (ArgumentException)
{
var flat = new PathSegment[collection.Count];
collection.CopyTo(flat, 0);
} Prevention
- Declare destination buffers as PathSegment[] (1D) rather than Array or multidimensional arrays.
- Validate array.Rank in shared copy helpers before delegating to CopyTo.
When it happens
Trigger: Calling pathSegmentCollection.CopyTo(array, index) where array was created with multiple dimensions, e.g. new PathSegment[2, 5].
Common situations: Reusing a multidimensional buffer across BCL interop code; generic copy helpers that accept Array and pass through whatever rank the caller supplied.
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
- SR.Collection_BadRank
- SR.Collection_BadRank
- Cannot pass multidimensional array to the CopyTo method on…
- SR.Argument_InvalidOffLen
- SR.Collection_BadDestArray
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/930de310536d27e1.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/PathSegmentCollection.cs:402
#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)