{"record":{"id":"84f3606fe30a6368","repo":"dotnet/wpf","slug":"sr-collection-badrank-textdecorationcollection","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/Generated/TextDecorationCollection.cs","lineNumber":393,"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":375,"sourceCodeEnd":411,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Generated/TextDecorationCollection.cs#L375-L411","documentation":"TextDecorationCollection.CopyTo throws ArgumentException (SR.Collection_BadRank) when the destination array has Rank != 1. CopyTo only supports single-dimensional (SZ) arrays, matching BCL collection conventions. The index bounds are already validated before this check.","triggerScenarios":"Passing a 2D array (e.g. TextDecoration[,] or Array.CreateInstance with rank 2) as the destination of CopyTo.","commonSituations":"Generic array-copy helper code that allocates multidimensional arrays; legacy code ported from ArrayList-era APIs that tolerated multidimensional arrays.","solutions":["Pass a single-dimensional array, e.g. new TextDecoration[count]","If you must use a multidimensional array, copy to a flat array first and then re-index manually","Fix array allocation at the call site to use rank-1 arrays"],"exampleFix":"// before\nvar arr = new TextDecoration[count, 1];\ncollection.CopyTo(arr, 0);\n// after\nvar arr = new TextDecoration[count];\ncollection.CopyTo(arr, 0);","handlingStrategy":"validation","validationCode":"if (array == null) throw new ArgumentNullException(nameof(array));\nif (array.Rank != 1) throw new ArgumentException(\"Destination array must be single-dimensional\", nameof(array));\nif (index < 0 || index + collection.Count > array.Length) throw new ArgumentException(\"Index out of range\");","typeGuard":"bool CanCopyTo(Array array) => array != null && array.Rank == 1 && array.Length - index >= collection.Count;","tryCatchPattern":null,"preventionTips":["Always allocate flat (rank-1) arrays for CopyTo targets","Check array.Rank before generic copy helpers call CopyTo","Keep index bounds: 0 <= index <= array.Length - Count"],"tags":["wpf","argumentexception","array-rank","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"}