{"record":{"id":"9a20f250297007b4","repo":"dotnet/wpf","slug":"sr-collection-baddestarray-collectionhelper","errorCode":null,"errorMessage":"SR.Collection_BadDestArray","messagePattern":"SR\\.Collection_BadDestArray","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WpfGfx/codegen/mcg/helpers/CollectionHelper.cs","lineNumber":384,"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                [[/inline]];\n\n        }\n\n        public static string WriteCopyToCommon(McgResource resource)\n        {\n            return\n                [[inline]]\n                    ReadPreamble();\n\n                    ArgumentNullException.ThrowIfNull(array);\n\n                    // This will not throw in the case that we are copying\n                    // from an empty collection.  This is consistent with the\n                    // BCL Collection implementations. (Windows 1587365)\n                    ArgumentOutOfRangeException.ThrowIfNegative(index);","sourceCodeStart":366,"sourceCodeEnd":402,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WpfGfx/codegen/mcg/helpers/CollectionHelper.cs#L366-L402","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nvar dest = new int[collection.Count];\ncollection.CopyTo(dest, 0); // BadDestArray\n// after\nvar dest = new MyElementType[collection.Count];\ncollection.CopyTo(dest, 0);","handlingStrategy":"validation","validationCode":"var elemType = array.GetType().GetElementType();\nif (elemType == null || !typeof(MyElementType).IsAssignableFrom(elemType))\n    throw new ArgumentException(\"array element type incompatible\");","typeGuard":null,"tryCatchPattern":"try { collection.CopyTo(array, index); }\ncatch (ArgumentException) { copyViaEnumeration(collection, array, index); }","preventionTips":["Type destination arrays as the collection's element type or a base type","Prefer the strongly typed CopyTo(T[], int) overload"],"tags":["wpf","collection","copyto","invalidcast","type-mismatch"],"backgroundTag":"type-mismatch","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"}