{"record":{"id":"6e063dc01f10445c","repo":"dotnet/wpf","slug":"sr-collection-baddestarray","errorCode":null,"errorMessage":"SR.Collection_BadDestArray","messagePattern":"SR\\.Collection_BadDestArray","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/DoubleCollection.cs","lineNumber":382,"sourceCode":"\n            if (array.Rank != 1)\n            {\n                throw new ArgumentException(SR.Collection_BadRank);\n            }\n\n            // Elsewhere in the collection we throw an AE when the type is\n            // bad so we do it here as well to be consistent\n            try\n            {\n                int count = _collection.Count;\n                for (int i = 0; i < count; i++)\n                {\n                    array.SetValue(_collection[i], index + i);\n                }\n            }\n            catch (InvalidCastException e)\n            {\n                throw new ArgumentException(SR.Format(SR.Collection_BadDestArray, this.GetType().Name), e);\n            }\n        }\n\n        bool ICollection.IsSynchronized\n        {\n            get\n            {\n                ReadPreamble();\n\n                return IsFrozen || Dispatcher != null;\n            }\n        }\n\n        object ICollection.SyncRoot\n        {\n            get\n            {\n                ReadPreamble();","sourceCodeStart":364,"sourceCodeEnd":400,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/DoubleCollection.cs#L364-L400","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nint[] dest = new int[coll.Count];\n((ICollection)coll).CopyTo(dest, 0); // ArgumentException: Collection_BadDestArray\n// after\ndouble[] dest = new double[coll.Count];\ncoll.CopyTo(dest, 0);","handlingStrategy":"type-guard","validationCode":"if (array == null) throw new ArgumentNullException(nameof(array));\nvar et = array.GetType().GetElementType();\nif (et != typeof(double) && !et.IsAssignableFrom(typeof(double)))\n    throw new ArgumentException(\"Destination array element type must accept double.\");","typeGuard":"static bool CanCopyInto(Array a) =>\n    a != null && a.GetType().GetElementType() is Type t && (t == typeof(double) || t.IsAssignableFrom(typeof(double)));","tryCatchPattern":"try { ((ICollection)coll).CopyTo(dest, 0); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Collection_BadDestArray\") || ex.ParamName == null && ex.InnerException is InvalidCastException)\n{ /* use a properly typed double[] and retry */ }","preventionTips":["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."],"tags":["wpf","collection","copyto","invalidcast"],"backgroundTag":"incompatible-source-type","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}