{"record":{"id":"5c045ad95f67eb01","repo":"dotnet/wpf","slug":"sr-collection-baddestarray-pathsegmentcollection","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/PathSegmentCollection.cs","lineNumber":417,"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":399,"sourceCodeEnd":435,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/PathSegmentCollection.cs#L399-L435","documentation":"PathSegmentCollection.CopyTo throws Collection_BadDestArray (as an ArgumentException) when one or more elements in the collection cannot be cast to the destination array's element type. The copy is performed via array.SetValue, and an InvalidCastException during that write is converted into this argument error, with the original cast failure as the InnerException.","triggerScenarios":"Calling CopyTo on a PathSegmentCollection with an array whose element type is not PathSegment and is not assignment-compatible (e.g. object[] is fine, but double[] or string[] is not) while the collection is non-empty.","commonSituations":"Copying into arrays typed as a base-of-PathSegment-less type, legacy code using non-generic ICollection.CopyTo with ArrayList or incorrectly typed buffers, or refactoring that changed the array type without updating the source collection type.","solutions":["Use a PathSegment[] (or PathSegment-compatible) array as the destination of CopyTo.","Check InnerException (InvalidCastException) to identify the offending element type.","Copy via the generic ICollection<PathSegment>.CopyTo or LINQ ToArray<PathSegment>() instead.","Ensure the array is large enough: index + Count must be within array bounds."],"exampleFix":"// before\nvar arr = new object[10];\ncollection.CopyTo((Array)arr, 0);\n// after\nvar arr = new PathSegment[collection.Count];\ncollection.CopyTo(arr, 0);","handlingStrategy":"validation","validationCode":"if (array == null) throw new ArgumentNullException(nameof(array));\nif (array.Rank != 1 || index < 0 || index + collection.Count > array.Length) throw new ArgumentException(\"Bad destination array\");\nif (array.GetType().GetElementType() != null && !typeof(PathSegment).IsAssignableFrom(array.GetType().GetElementType())) throw new ArgumentException(\"Array element type must be PathSegment-compatible\");","typeGuard":"static bool IsCompatibleDestArray(Array a) => a.Rank == 1 && typeof(PathSegment).IsAssignableFrom(a.GetType().GetElementType());","tryCatchPattern":"try { collection.CopyTo(arr, 0); }\ncatch (ArgumentException ex) when (ex.InnerException is InvalidCastException) { /* fall back to typed copy */ }","preventionTips":["Always allocate destination arrays with the collection's element type","Prefer generic ICollection<T>.CopyTo or ToArray()","Check InnerException to diagnose cast failures"],"tags":["wpf","collection","argumentexception"],"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"}