dotnet/wpf · error · ArgumentException
SR.Collection_BadDestArray
Error message
SR.Collection_BadDestArray
What it means
This SR resource backs an ArgumentException (wrapping an InvalidCastException) thrown by the generated ICollection.CopyTo(Array, int) implementation (CollectionHelper.cs, Collection_SetValue) when a collection element cannot be stored in the destination array's element type. The message includes the collection type name to aid diagnosis.
Solutions
- Allocate the destination array with the collection's element type (or a compatible base type)
- Validate array element-type compatibility before calling CopyTo
- Use CopyTo(T[], int) or enumeration instead of the Array overload when types differ
- Catch ArgumentException and surface the collection type name to diagnose the mismatch
Example fix
// before var dest = new int[collection.Count]; collection.CopyTo(dest, 0); // BadDestArray // after var dest = new MyElementType[collection.Count]; collection.CopyTo(dest, 0);
Defensive patterns
Strategy: validation
Validate before calling
var elemType = array.GetType().GetElementType();
if (elemType == null || !typeof(MyElementType).IsAssignableFrom(elemType))
throw new ArgumentException("array element type incompatible"); Try / catch
try { collection.CopyTo(array, index); }
catch (ArgumentException) { copyViaEnumeration(collection, array, index); } Prevention
- Type destination arrays as the collection's element type or a base type
- Prefer the strongly typed CopyTo(T[], int) overload
When it happens
Trigger: Calling CopyTo with a destination array whose element type is not assignable from the collection's element type (e.g. copying a typed collection into an incompatible array), causing Array.SetValue to throw InvalidCastException which is wrapped.
Common situations: Passing narrowly typed arrays through object[]-typed code paths; covariance assumptions between similar element types; refactoring element types so old call sites' arrays no longer match.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- SR.Collection_BadDestArray
- SR.Collection_BadDestArray
- SR.Collection_BadDestArray
- SR.Collection_BadDestArray
- SR.Format(SR.Collection_BadDestArray, "Visual3DCollection")
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/9a20f250297007b4.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WpfGfx/codegen/mcg/helpers/CollectionHelper.cs:384
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);
}
}
[[/inline]];
}
public static string WriteCopyToCommon(McgResource resource)
{
return
[[inline]]
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);View on GitHub (pinned to 81131a70a4)