dotnet/wpf · error · ArgumentException
SR.Collection_BadDestArray
Error message
SR.Collection_BadDestArray
What it means
DoubleCollection.CopyTo (explicit ICollection.CopyTo) wraps any InvalidCastException raised while copying elements into the destination Array via array.SetValue, and rethrows it as ArgumentException with SR.Collection_BadDestArray naming the collection type. The library throws this because the destination array's element type cannot accept the double values stored in the collection.
Solutions
- Use a double[] (or Array with a double-compatible element type) as the destination array.
- Use the strongly typed CopyTo(double[] array, int index) overload instead of the ICollection one.
- Verify the element type with array.GetType().GetElementType() before copying, or copy via foreach into a converted array.
Example fix
// before int[] dest = new int[coll.Count]; ((ICollection)coll).CopyTo(dest, 0); // ArgumentException: Collection_BadDestArray // after double[] dest = new double[coll.Count]; coll.CopyTo(dest, 0);
Defensive patterns
Strategy: type-guard
Validate before calling
if (array == null) throw new ArgumentNullException(nameof(array));
var et = array.GetType().GetElementType();
if (et != typeof(double) && !et.IsAssignableFrom(typeof(double)))
throw new ArgumentException("Destination array element type must accept double."); Type guard
static bool CanCopyInto(Array a) =>
a != null && a.GetType().GetElementType() is Type t && (t == typeof(double) || t.IsAssignableFrom(typeof(double))); Try / catch
try { ((ICollection)coll).CopyTo(dest, 0); }
catch (ArgumentException ex) when (ex.Message.Contains("Collection_BadDestArray") || ex.ParamName == null && ex.InnerException is InvalidCastException)
{ /* use a properly typed double[] and retry */ } Prevention
- Prefer the strongly typed CopyTo(double[], int) overload.
- Never copy primitive collections into differently-typed arrays.
- Type destination buffers as double[] at declaration sites.
- Add a unit test for collection CopyTo with the exact destination array type.
When it happens
Trigger: Calling ((ICollection)doubleCollection).CopyTo(array, index) where array's element type is not compatible with double (e.g. an int[], string[], or object[] with restricted casting via SetValue on arrays of non-convertible types), causing SetValue to throw InvalidCastException.
Common situations: Passing an array typed for a different primitive (int[] instead of double[]) after a refactor; interop code copying into arrays of a mistyped element type; generic collection-copy helper that boxes into the wrong array type.
Understand the failure class
Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.
Related errors
- SR.Collection_BadDestArray
- SR.Collection_BadDestArray
- SR.Collection_BadDestArray
- Cannot pass multidimensional array to the CopyTo method on…
- SR.Collection_BadDestArray
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/6e063dc01f10445c.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/DoubleCollection.cs:382
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);
}
}
bool ICollection.IsSynchronized
{
get
{
ReadPreamble();
return IsFrozen || Dispatcher != null;
}
}
object ICollection.SyncRoot
{
get
{
ReadPreamble();View on GitHub (pinned to 81131a70a4)