{"record":{"id":"7db662f040ed24ea","repo":"dotnet/wpf","slug":"sr-arg-rankmultidimnotsupported","errorCode":null,"errorMessage":"SR.Arg_RankMultiDimNotSupported","messagePattern":"SR\\.Arg_RankMultiDimNotSupported","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Collections/ObjectModel/WeakReadOnlyCollection.cs","lineNumber":141,"sourceCode":"                    if (list is ICollection c)\n                    {\n                        _syncRoot = c.SyncRoot;\n                    }\n                    else\n                    {\n                        System.Threading.Interlocked.CompareExchange<Object>(ref _syncRoot, new Object(), null);\n                    }\n                }\n                return _syncRoot;\n            }\n        }\n\n        void ICollection.CopyTo(Array array, int index) {\n            ArgumentNullException.ThrowIfNull(array);\n\n            if (array.Rank != 1) {\n                //ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);\n                throw new ArgumentException(SR.Arg_RankMultiDimNotSupported);\n            }\n\n            if( array.GetLowerBound(0) != 0 ) {\n                //ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_NonZeroLowerBound);\n                throw new ArgumentException(SR.Arg_NonZeroLowerBound);\n            }\n\n            if (index < 0) {\n                //ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.arrayIndex, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);\n                throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_NeedNonNegNum);\n            }\n\n            if (array.Length - index < Count) {\n                //ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);\n                throw new ArgumentException(SR.Arg_ArrayPlusOffTooSmall);\n            }\n\n            IList<T> dlist = CreateDereferencedList();","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Collections/ObjectModel/WeakReadOnlyCollection.cs#L123-L159","documentation":"ICollection.CopyTo requires a single-dimensional, zero-based array; the library throws ArgumentException with SR.Arg_RankMultiDimNotSupported when array.Rank != 1. Multi-dimensional arrays cannot be indexed with a single flat offset, so the copy would be ambiguous.","triggerScenarios":"Calling CopyTo on WeakReadOnlyCollection (explicit ICollection.CopyTo) passing a 2D array like new WeakReference[3,3] or any array with Rank > 1.","commonSituations":"Legacy .NET code or interop scenarios that still use rectangular arrays; passing a buffer created as T[,] for chunked copying.","solutions":["Pass a single-dimensional zero-based array (T[] or object[])","Copy into a flat 1D array and translate to the 2D target manually","Guard with array.Rank == 1 before calling CopyTo"],"exampleFix":"// before\nvar grid = new WeakReference[count, count];\ncollection.CopyTo(grid, 0);\n// after\nvar flat = new WeakReference[count * count];\ncollection.CopyTo(flat, 0);","handlingStrategy":"validation","validationCode":"if (array == null) throw new ArgumentNullException(nameof(array));\nif (array.Rank != 1) throw new ArgumentException(\"CopyTo requires a single-dimensional array.\", nameof(array));","typeGuard":"bool IsFlatZeroBased(Array a) => a.Rank == 1;","tryCatchPattern":"try { ((ICollection)collection).CopyTo(array, index); } catch (ArgumentException e) when (e.Message.Contains(\"Rank\")) { /* flatten to 1D and retry */ }","preventionTips":["Always allocate 1D arrays for CopyTo","Convert T[,] buffers to flat T[] with Buffer/loop copying","Add an assert on array.Rank in copy helpers"],"tags":["wpf","copyto","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-21T21:30:21.729Z"}