{"record":{"id":"b256e0bee7577416","repo":"dotnet/wpf","slug":"sr-collection-baddestarray-pointcollection","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/PointCollection.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/PointCollection.cs#L364-L400","documentation":"PointCollection.CopyTo throws ArgumentException (wrapping InvalidCastException) when the destination array cannot hold Point elements, i.e. WPF cannot cast every collected value into the destination array's element type. The library catches the InvalidCastException from array.SetValue and rethrows it as ArgumentException with Collection_BadDestArray, naming the collection type. It signals the destination array's type is incompatible with the collection's element type, not a size problem.","triggerScenarios":"Calling PointCollection.CopyTo (explicit ICollection.CopyTo or the generic overload path that shares it) with an array whose element type is not Point and not assignable from Point, e.g. copying a PointCollection into an object[] whose runtime type disallows Point, or a string[]/int[].","commonSituations":"Passing a loosely typed Array variable that was actually allocated as a non-Point array; marshaling old code that used ArrayList-style copies into typed arrays; refactoring that changed the destination array's element type without updating the CopyTo call.","solutions":["Allocate the destination array as Point[] (or a type Point is assignable to) before calling CopyTo.","If you need a different element type, project first: points.Select(p => p.ToString()).ToArray() then copy.","Verify array.GetElementType() compatibility before copying (see validation code)."],"exampleFix":"// before\nArray dest = new string[points.Count];\npoints.CopyTo(dest, 0); // ArgumentException Collection_BadDestArray\n// after\nvar dest = new Point[points.Count];\npoints.CopyTo(dest, 0);","handlingStrategy":"validation","validationCode":"static bool CanCopyTo(PointCollection c, Array dest) => dest != null && (dest.GetElementType()?.IsAssignableFrom(typeof(Point)) ?? false);","typeGuard":"static bool IsPointArray(Array a) => a?.GetElementType() == typeof(Point);","tryCatchPattern":"try { points.CopyTo(dest, 0); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Collection_BadDestArray\")) { /* recreate dest as Point[] */ }","preventionTips":["Always allocate CopyTo destinations as the collection's exact element type (Point[]).","Avoid holding destination arrays as plain Array variables; keep them strongly typed.","When converting element types, use LINQ projection instead of CopyTo into a differently typed array."],"tags":["wpf","collection","argumentexception","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-21T21:30:21.729Z"}