{"record":{"id":"bbbb35f5fef68cdd","repo":"dotnet/wpf","slug":"sr-collection-badrank-freezablecollection","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/FreezableCollection.cs","lineNumber":481,"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":463,"sourceCodeEnd":499,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/FreezableCollection.cs#L463-L499","documentation":"FreezableCollection(T).CopyTo(T[] array, int index) throws ArgumentException (SR.Collection_BadRank) when the target array has more than one dimension. CopyTo requires a single-dimensional (rank 1) array so elements can be copied linearly.","triggerScenarios":"Calling CopyTo with a 2D array such as new T[2,3].","commonSituations":"Reusing a multidimensional buffer allocated elsewhere; porting code that previously used a different collection allowing multi-rank arrays.","solutions":["Pass a one-dimensional array (T[count]) to CopyTo.","Allocate a fresh 1D array and copy from the multidimensional array manually.","Check array.Rank == 1 before calling."],"exampleFix":"// before\nvar buffer = new Geometry[2, 10];\ncollection.CopyTo(buffer, 0);\n// after\nvar buffer = new Geometry[collection.Count];\ncollection.CopyTo(buffer, 0);","handlingStrategy":"validation","validationCode":"if (array.Rank != 1) throw new InvalidOperationException(\"CopyTo requires a 1D array\");","typeGuard":null,"tryCatchPattern":"try { collection.CopyTo(array, 0); } catch (ArgumentException) { /* allocate 1D array and retry */ }","preventionTips":["Always pass single-dimensional arrays to CopyTo.","Check array.Rank before copying.","Allocate dedicated 1D buffers for collection copies."],"tags":["wpf","freezable","collection","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"}