{"record":{"id":"538895d1cbbaef67","repo":"dotnet/wpf","slug":"sr-collection-badrank-pointcollection","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/PointCollection.cs","lineNumber":367,"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":349,"sourceCodeEnd":385,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/PointCollection.cs#L349-L385","documentation":"PointCollection.CopyTo throws Collection_BadRank (ArgumentException) when the destination array has a rank other than 1 (multi-dimensional array). WPF collection CopyTo implementations only support single-dimensional arrays, matching BCL conventions.","triggerScenarios":"Calling CopyTo on a PointCollection passing a multidimensional array, e.g. new Point[2,2], as the destination.","commonSituations":"Legacy COM/WinForms-era code using rectangular arrays, ported code assuming multidimensional copy support, or generic array helpers that accept Array and allocate by dimensions.","solutions":["Use a one-dimensional array: new Point[collection.Count].","Copy via ToArray() or the generic ICollection<Point>.CopyTo.","If a 2D layout is needed, copy into a flat array and index it manually.","Validate array.Rank == 1 before calling CopyTo in generic helpers."],"exampleFix":"// before\nvar grid = new Point[2, 2];\npoints.CopyTo(grid, 0); // throws\n// after\nvar flat = new Point[points.Count];\npoints.CopyTo(flat, 0);","handlingStrategy":"validation","validationCode":"if (array == null || array.Rank != 1 || index < 0 || index + points.Count > array.Length)\n    throw new ArgumentException(\"Destination must be a 1-D array with sufficient capacity\");","typeGuard":"static bool IsFlatArray(Array a) => a.Rank == 1;","tryCatchPattern":"try { points.CopyTo(arr, 0); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"rank\")) { /* use 1-D array */ }","preventionTips":["Always use single-dimensional destination arrays","Prefer ToArray()/generic CopyTo over Array-typed overloads","Check array.Rank in generic helper methods before calling CopyTo"],"tags":["wpf","collection","argumentexception"],"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"}