{"record":{"id":"6ec9a39e8d9e9784","repo":"dotnet/wpf","slug":"sr-collection-badrank-doublecollection","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/DoubleCollection.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/DoubleCollection.cs#L349-L385","documentation":"DoubleCollection.CopyTo throws ArgumentException when the destination array is multi-dimensional (Rank != 1). ICollection<T>.CopyTo requires a single-dimensional array with zero-based indexing, and WPF enforces this with SR.Collection_BadRank after validating the index range.","triggerScenarios":"Calling DoubleCollection.CopyTo(array, index) where array.Rank != 1, e.g. a 2D double[,] array; also index < 0 or index > array.Length - Count (those throw ArgumentOutOfRangeException first).","commonSituations":"Copying a DoubleCollection (points, stroke dash data) into a rectangular 2D array assumed to be flattenable; passing buffers from legacy code shaped as multidimensional arrays.","solutions":["Pass a single-dimensional double[] sized to Count plus the starting index.","Flatten the multidimensional array first or copy element-by-element.","Validate array.Rank == 1 before calling CopyTo.","Use LINQ ToArray() to obtain a 1D array from the collection instead."],"exampleFix":"// before\nvar rect = new double[points.Count, 2];\ndoubleCollection.CopyTo(rect, 0); // ArgumentException: rank\n// after\nvar flat = new double[points.Count];\ndoubleCollection.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 1-dimensional\");\nif (index < 0 || index > array.Length - collection.Count) throw new ArgumentOutOfRangeException(nameof(index));","typeGuard":"bool isValidTarget = array is double[] oneDim && index >= 0 && index + collection.Count <= oneDim.Length;","tryCatchPattern":"try { collection.CopyTo(array, index); } catch (ArgumentException) { /* array was multidimensional; copy element-wise */ for (int i = 0; i < collection.Count; i++) flat[index + i] = collection[i]; }","preventionTips":["Always allocate a 1D double[] sized Count + index before CopyTo.","Never pass T[,] or T[,,] arrays to ICollection<T>.CopyTo.","Use ToArray()/ToList() instead of manual buffer copies.","Check array.Rank == 1 in helper wrappers around CopyTo."],"tags":["wpf","collections","argument-exception","array-rank"],"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"}