{"record":{"id":"573c2bc806b58f98","repo":"dotnet/wpf","slug":"sr-collection-badrank-generaltransformcollection","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/Media/Generated/GeneralTransformCollection.cs","lineNumber":402,"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":384,"sourceCodeEnd":420,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/GeneralTransformCollection.cs#L384-L420","documentation":"Thrown by GeneralTransformCollection.CopyTo when the target array is multidimensional (array.Rank != 1). The WPF collection classes only support copying into one-dimensional arrays, matching standard BCL ICollection.CopyTo behavior.","triggerScenarios":"Calling CopyTo(int index, System.Array array) with a 2D array such as new GeneralTransform[3,3], or any array created with Array.CreateInstance(type, lengths, lowerBounds) with more than one dimension.","commonSituations":"Copying a collection into a matrix-shaped buffer, reusing a rectangular array that was used for tabular data, or generically calling ICollection.CopyTo via reflection with an arbitrary Array.","solutions":["Pass a one-dimensional GeneralTransform[] sized to collection.Count plus the desired index offset","Check array.Rank == 1 before calling CopyTo and allocate a flat array if not","Marshal data manually with nested loops if a multidimensional layout is required"],"exampleFix":"// before\nvar matrix = new GeneralTransform[2, 2];\ncollection.CopyTo(0, matrix);\n// after\nvar flat = new GeneralTransform[collection.Count];\ncollection.CopyTo(0, flat);","handlingStrategy":"validation","validationCode":"if (target == null) throw new ArgumentNullException(nameof(target));\nif (target.Rank != 1) throw new ArgumentException(\"Destination array must be one-dimensional\", nameof(target));\nif (index + collection.Count > target.Length) throw new ArgumentException(\"Destination array too small\", nameof(target));","typeGuard":"static bool IsUsableCopyTarget(Array a, int index, int count) => a is { Rank: 1 } && index >= 0 && index + count <= a.Length;","tryCatchPattern":"try { collection.CopyTo(0, array); } catch (ArgumentException ex) when (array.Rank != 1) { /* fall back to flat array */ }","preventionTips":["Always allocate copy targets as single-dimensional arrays","Validate array.Rank before ICollection.CopyTo calls in generic code","Document expected array shape in helper APIs"],"tags":["wpf","argumentexception","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-22T01:17:13.364Z"}