{"record":{"id":"930de310536d27e1","repo":"dotnet/wpf","slug":"sr-collection-badrank-pathsegmentcollection","errorCode":null,"errorMessage":"SR.Collection_BadRank","messagePattern":"SR\\.Collection_BadRank","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/PathSegmentCollection.cs","lineNumber":402,"sourceCode":"        #endregion\n\n        #region ICollection\n\n        void ICollection.CopyTo(Array array, int index)\n        {\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);\n            ArgumentOutOfRangeException.ThrowIfGreaterThan(index, array.Length - _collection.Count);\n\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","sourceCodeStart":384,"sourceCodeEnd":420,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/PathSegmentCollection.cs#L384-L420","documentation":"PathSegmentCollection.CopyTo throws ArgumentException with SR.Collection_BadRank when the destination array is not one-dimensional (Rank != 1). CopyTo requires a single-dimensional Array because it writes elements by linear index.","triggerScenarios":"Calling pathSegmentCollection.CopyTo(array, index) where array was created with multiple dimensions, e.g. new PathSegment[2, 5].","commonSituations":"Reusing a multidimensional buffer across BCL interop code; generic copy helpers that accept Array and pass through whatever rank the caller supplied.","solutions":["Use a one-dimensional PathSegment[] as the destination.","If you need 2D storage, copy into a flat 1D array first and then reshape manually.","Check array.Rank == 1 before calling CopyTo and handle the multidimensional case separately."],"exampleFix":"// before\nvar grid = new PathSegment[2, 5];\ncollection.CopyTo(grid, 0);\n// after\nvar flat = new PathSegment[collection.Count];\ncollection.CopyTo(flat, 0);","handlingStrategy":"validation","validationCode":"if (array == null) throw new ArgumentNullException(nameof(array));\nif (array.Rank != 1) throw new ArgumentException(\"Destination array must be one-dimensional\", nameof(array));\ncollection.CopyTo(array, index);","typeGuard":"bool IsFlatSegmentArray(Array a) => a is PathSegment[];","tryCatchPattern":"try\n{\n    collection.CopyTo(array, 0);\n}\ncatch (ArgumentException)\n{\n    var flat = new PathSegment[collection.Count];\n    collection.CopyTo(flat, 0);\n}","preventionTips":["Declare destination buffers as PathSegment[] (1D) rather than Array or multidimensional arrays.","Validate array.Rank in shared copy helpers before delegating to CopyTo."],"tags":["wpf","collection","array","copyto"],"backgroundTag":"invalid-argument-value","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"}