{"record":{"id":"52545a8298f5ba1c","repo":"dotnet/wpf","slug":"sr-collection-badrank-visual3dcollection","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/Visual3DCollection.cs","lineNumber":159,"sourceCode":"            _collection.CopyTo(array, index);\n        }\n\n        void ICollection.CopyTo(Array array, int index)\n        {\n            VerifyAPIReadOnly();\n\n            ArgumentNullException.ThrowIfNull(array);\n\n            // The extra \"index >= array.Length\" check in because even if _collection.Count\n            // is 0 the index is not allowed to be equal or greater than the length\n            // (from the MSDN ICollection docs)\n            ArgumentOutOfRangeException.ThrowIfNegative(index);\n            ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, array.Length);\n            ArgumentOutOfRangeException.ThrowIfGreaterThan(index, array.Length - 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, \"Visual3DCollection\"), e);\n            }\n        }\n","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media3D/Visual3DCollection.cs#L141-L177","documentation":"Visual3DCollection.CopyTo validates the destination array and throws ArgumentException(SR.Collection_BadRank) when array.Rank != 1, i.e. the target is a multi-dimensional array. IList.CopyTo in WPF collections only supports single-dimensional (rank-1) arrays.","triggerScenarios":"Calling visual3DCollection.CopyTo(multidimArray, 0) with e.g. a Visual3D[,] destination; passing an array created via Array.CreateInstance with rank > 1.","commonSituations":"Generic serialization/data-binding code that allocates Array.CreateInstance(typeof(Visual3D), lengths) with multiple dimensions; ported code from collections that accepted multidimensional arrays.","solutions":["Pass a single-dimensional Visual3D[] sized to Count (or larger) as the destination","If a multidimensional result is needed, copy into a 1D array first, then re-shape manually","Guard with array.Rank == 1 before calling CopyTo"],"exampleFix":"// before\nvar arr = Array.CreateInstance(typeof(Visual3D), new[] { n, 1 });\ncollection.CopyTo(arr, 0);\n// after\nvar arr = new Visual3D[collection.Count];\ncollection.CopyTo(arr, 0);","handlingStrategy":"validation","validationCode":"if (dest is Array a && a.Rank == 1 && a.Length - index >= collection.Count) collection.CopyTo(a, index);","typeGuard":"static bool IsValidDest(Visual3DCollection c, Visual3D[] dest, int index) => dest != null && index >= 0 && dest.Length - index >= c.Count;","tryCatchPattern":"try { collection.CopyTo(arr, 0); } catch (ArgumentException) { arr = new Visual3D[collection.Count]; collection.CopyTo(arr, 0); }","preventionTips":["Always allocate single-dimensional Visual3D[] for CopyTo destinations","Never pass Array.CreateInstance results with rank > 1","Size the destination >= Count plus index offset"],"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"}