{"record":{"id":"de1bd85df1d6f1a5","repo":"dotnet/wpf","slug":"sr-badtargetarray","errorCode":null,"errorMessage":"SR.BadTargetArray","messagePattern":"SR\\.BadTargetArray","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemCollection.cs","lineNumber":286,"sourceCode":"\n            return _collectionView.Contains(containItem);\n        }\n\n        /// <summary>\n        ///     Makes a shallow copy of object references from this\n        ///     ItemCollection to the given target array\n        /// </summary>\n        /// <param name=\"array\">\n        ///     Target of the copy operation\n        /// </param>\n        /// <param name=\"index\">\n        ///     Zero-based index at which the copy begins\n        /// </param>\n        public void CopyTo(Array array, int index)\n        {\n            ArgumentNullException.ThrowIfNull(array);\n            if (array.Rank > 1)\n                throw new ArgumentException(SR.BadTargetArray, nameof(array)); // array is multidimensional.\n            ArgumentOutOfRangeException.ThrowIfNegative(index);\n\n            // use the view instead of the collection, because it may have special sort/filter\n            if (!EnsureCollectionView())\n                return;  // there is no collection (bind returned no collection) and therefore nothing to copy\n\n            VerifyRefreshNotDeferred();\n\n            IndexedEnumerable.CopyTo(_collectionView, array, index);\n        }\n\n        /// <summary>\n        ///     Finds the index in this collection/view where the given item is found.\n        /// </summary>\n        /// <param name=\"item\">\n        ///     The item whose index in this collection/view is to be retrieved.\n        /// </param>\n        /// <returns>","sourceCodeStart":268,"sourceCodeEnd":304,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemCollection.cs#L268-L304","documentation":"ItemCollection.CopyTo throws ArgumentException(SR.BadTargetArray) when the destination Array has more than one dimension. .NET copy operations require a single-dimensional (SZ) array so elements can be laid out sequentially. The library rejects multidimensional arrays up front before touching the collection view.","triggerScenarios":"Calling ItemCollection.CopyTo(Array array, int index) with an array created as e.g. new object[2,3] (Rank > 1).","commonSituations":"Reusing a legacy 2D buffer or a rectangular array from older code to collect ItemsControl items; interop code that builds grid-like arrays and assumes CopyTo accepts any Array.","solutions":["Pass a single-dimensional array such as new object[itemsControl.Items.Count].","If you need 2D layout, copy into a flat 1D array first and then map indices yourself.","Guard with array.Rank == 1 before calling CopyTo."],"exampleFix":"// before\nvar arr = new object[items.Count, 2];\nitems.CopyTo(arr, 0);\n// after\nvar arr = new object[items.Count];\nitems.CopyTo(arr, 0);","handlingStrategy":"validation","validationCode":"if (array == null || array.Rank != 1)\n    throw new ArgumentException(\"Destination must be a single-dimensional array\", nameof(array));\nitems.CopyTo(array, 0);","typeGuard":"static bool IsSingleDimensional(Array a) => a != null && a.Rank == 1;","tryCatchPattern":"try { items.CopyTo(array, 0); }\ncatch (ArgumentException ex) when (ex.ParamName == \"array\") { /* use a 1D array */ }","preventionTips":["Always allocate SZ (new T[n]) arrays as CopyTo targets","Check Array.Rank before copy operations","Avoid reusing legacy multidimensional buffers for collection copies"],"tags":["wpf","itemcollection","argumentexception","array"],"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"}