dotnet/wpf · error · ArgumentException
SR.GridCollection_DestArrayInvalidRank
Error message
SR.GridCollection_DestArrayInvalidRank
What it means
RowDefinition's explicit ICollection.CopyTo implementation validates the destination array before copying. A multi-dimensional (Rank != 1) array cannot be used as the copy destination, so it throws ArgumentException(SR.GridCollection_DestArrayInvalidRank).
Solutions
- Pass a single-dimensional array, e.g. new RowDefinition[grid.RowDefinitions.Count].
- Cast multidimensional data to a flat 1D array first.
- Add a Rank == 1 check in calling code to fail early with a clearer message.
Example fix
// before var dest = new RowDefinition[rows, 1]; ((ICollection)grid.RowDefinitions).CopyTo(dest, 0); // after var dest = new RowDefinition[grid.RowDefinitions.Count]; ((ICollection)grid.RowDefinitions).CopyTo(dest, 0);
Defensive patterns
Strategy: type-guard
Validate before calling
if (dest == null) throw new ArgumentNullException(nameof(dest)); if (dest.Rank != 1) dest = new RowDefinition[grid.RowDefinitions.Count]; ((ICollection)grid.RowDefinitions).CopyTo(dest, 0);
Type guard
bool IsSingleDimensional(Array a) => a.Rank == 1;
Try / catch
try { ((ICollection)grid.RowDefinitions).CopyTo(dest, 0); }
catch (ArgumentException) { dest = new RowDefinition[grid.RowDefinitions.Count]; ((ICollection)grid.RowDefinitions).CopyTo(dest, 0); } Prevention
- Always allocate one-dimensional arrays for ICollection.CopyTo destinations.
- Avoid generic Array-typed scratch buffers that may be multidimensional.
- Use the strongly-typed CopyTo/ToArray style helpers when available.
When it happens
Trigger: Calling CopyTo (or collection initializers / APIs that use it) on a RowDefinitionCollection with a 2D array: ((ICollection)grid.RowDefinitions).CopyTo(twoDimArray, 0).
Common situations: Reusing a Rect[,] or object[,] scratch array for the copy; generic serialization code that allocates multidimensional arrays; interop code assuming any Array works.
Related errors
- SR.GridCollection_DestArrayInvalidLength
- SR.Collection_NoNull
- SR.Format(SR.Collection_BadType, this.GetType().Name…
- SR.Format(SR.GridCollection_InOtherCollection…
- SR.Format(SR.GridCollection_InOtherCollection, "value"…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/a800c4fc064d4ecc.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RowDefinition.cs:73
#endregion Constructors
//------------------------------------------------------
//
// Public Methods
//
//------------------------------------------------------
#region Public Methods
/// <summary>
/// <see cref="ICollection.CopyTo"/>
/// </summary>
void ICollection.CopyTo(Array array, int index)
{
ArgumentNullException.ThrowIfNull(array);
if (array.Rank != 1)
{
throw new ArgumentException(SR.GridCollection_DestArrayInvalidRank);
}
if (index < 0)
{
throw new ArgumentOutOfRangeException(SR.Format(SR.GridCollection_DestArrayInvalidLowerBound, "index"));
}
if (array.Length - index < _size)
{
throw new ArgumentException(SR.Format(SR.GridCollection_DestArrayInvalidLength, "array"));
}
if (_size > 0)
{
Debug.Assert(_items != null);
Array.Copy(_items, 0, array, index, _size);
}
}
/// <summary>View on GitHub (pinned to 81131a70a4)