{"record":{"id":"55dcd13fa73c80b6","repo":"dotnet/wpf","slug":"sr-collection-badrank-texteffectcollection","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/TextEffectCollection.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/TextEffectCollection.cs#L384-L420","documentation":"TextEffectCollection.CopyTo throws ArgumentException(Collection_BadRank) when the destination array has Rank != 1, i.e. it is multidimensional (e.g. TextEffect[_,_]). WPF collections only support copying into single-dimensional arrays, consistent with other BCL collection implementations (per the Windows 1587365 note). The bounds checks (negative index, index + count beyond Length) run first and throw ArgumentOutOfRangeException separately.","triggerScenarios":"Calling CopyTo with a 2D (or higher) array such as new TextEffect[2,2], or an Array-typed variable that happens to be multidimensional; note index out of bounds on a 1D array throws ArgumentOutOfRangeException before the rank check.","commonSituations":"Generic copy helpers written against Array that receive rectangular arrays; legacy code copying into matrices; APIs that allocate arrays dynamically and produce rank-2 arrays.","solutions":["Use a single-dimensional array: new TextEffect[collection.Count].","Flatten a multidimensional array first (or copy into a flat temp array then reshape).","Check array.Rank == 1 before calling CopyTo."],"exampleFix":"// before\nvar dest = new TextEffect[2, 2];\ncollection.CopyTo(dest, 0); // ArgumentException Collection_BadRank\n// after\nvar dest = new TextEffect[collection.Count];\ncollection.CopyTo(dest, 0);","handlingStrategy":"validation","validationCode":"static bool CanCopyTo(Array dest, int index, int count) => dest != null && dest.Rank == 1 && index >= 0 && index + count <= dest.Length;","typeGuard":"static bool IsSingleDimensional(Array a) => a?.Rank == 1;","tryCatchPattern":"try { collection.CopyTo(dest, index); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Collection_BadRank\")) { /* recreate dest as rank-1 array */ }","preventionTips":["Always copy into new T[collection.Count] — single-dimensional by construction.","Reject multidimensional arrays early in generic Array-based helpers with a Rank == 1 check.","Also pre-check index bounds (index >= 0 && index + count <= array.Length) since those throw ArgumentOutOfRangeException first."],"tags":["wpf","collection","argumentexception","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-21T21:30:21.729Z"}