{"record":{"id":"2d242a6109c45386","repo":"dotnet/wpf","slug":"sr-collection-badrank-model3dcollection","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/Media3D/Generated/Model3DCollection.cs","lineNumber":412,"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":394,"sourceCodeEnd":430,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media3D/Generated/Model3DCollection.cs#L394-L430","documentation":"Model3DCollection.CopyTo(array, index) requires a single-dimensional (rank-1) destination array and throws ArgumentException(SR.Collection_BadRank) otherwise. CopyTo uses Array.SetValue on the target, which cannot address multi-dimensional arrays with a linear index. The BCL convention for ICollection.CopyTo is to reject multidimensional arrays.","triggerScenarios":"Calling CopyTo on a Model3DCollection with a 2D (or higher) array as the destination; also possible if index > array.Length - Count, though that throws ArgumentOutOfRangeException first.","commonSituations":"Reusing a rectangular Model3D[,] buffer used elsewhere; generic serialization helpers that accept arbitrary arrays.","solutions":["Pass a one-dimensional Model3D[] of length >= Count + index.","Allocate a new Model3D[collection.Count] for the copy.","Refactor surrounding code that stores models in multidimensional arrays to use jagged arrays or lists."],"exampleFix":"// before\nvar target = new Model3D[2, 2];\ncollection.CopyTo(target, 0); // ArgumentException: bad rank\n// after\nvar target = new Model3D[collection.Count];\ncollection.CopyTo(target, 0);","handlingStrategy":"validation","validationCode":"if (array == null || array.Rank != 1 || array.Length < index + collection.Count) throw new ArgumentException(\"Destination must be a 1D array with sufficient length.\");","typeGuard":"bool CanCopyTo(Array a, int i) => a != null && a.Rank == 1 && i >= 0 && a.Length - i >= collection.Count;","tryCatchPattern":"try { collection.CopyTo(array, index); } catch (ArgumentException ex) { // bad rank or bad dest array\n    var dest = new Model3D[collection.Count]; collection.CopyTo(dest, 0); }","preventionTips":["Always use flat Model3D[] buffers for CopyTo","Validate Rank and capacity before copying","Avoid reusing multidimensional scratch buffers for ICollection copies"],"tags":["wpf","collection","copyto","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-22T01:17:13.364Z"}