{"record":{"id":"5a4310114b9854d7","repo":"dotnet/wpf","slug":"collection-badrank","errorCode":null,"errorMessage":"Collection_BadRank","messagePattern":"Collection_BadRank","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphTypeface.cs","lineNumber":1813,"sourceCode":"            }\n\n            public void Clear()\n            {\n                throw new NotSupportedException();\n            }\n\n            public bool Contains(KeyValuePair<ushort, double> item)\n            {\n                return ContainsKey(item.Key);\n            }\n\n            public void CopyTo(KeyValuePair<ushort, double>[] array, int arrayIndex)\n            {\n                ArgumentNullException.ThrowIfNull(array);\n\n                if (array.Rank != 1)\n                {\n                    throw new ArgumentException(SR.Collection_BadRank);\n                }\n\n                // The extra \"arrayIndex >= 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(arrayIndex);\n                ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(arrayIndex, array.Length);\n                ArgumentOutOfRangeException.ThrowIfGreaterThan(arrayIndex, array.Length - Count);\n\n                for (ushort i = 0; i < Count; ++i)\n                    array[arrayIndex + i] = new KeyValuePair<ushort, double>(i, this[i]);\n            }\n\n            public int Count\n            {\n                get { return _numberOfGlyphs; }\n            }\n","sourceCodeStart":1795,"sourceCodeEnd":1831,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphTypeface.cs#L1795-L1831","documentation":"GlyphTypeface's ICollection<KeyValuePair<ushort,double>> CopyTo implementation throws ArgumentException with resource 'Collection_BadRank' when the destination array is multi-dimensional. .NET collection copy contracts only support single-dimensional (rank 1) arrays, so a jagged rank check is enforced before copying advance ratios.","triggerScenarios":"Calling GlyphTypeface.AdvanceMetrics.CopyTo(...) (or the explicit interface CopyTo) with a 2D array (e.g. new double[3,3] cast or a KeyValuePair<ushort,double>[,]) as the destination array.","commonSituations":"Developers reusing a multidimensional scratch buffer for glyph metrics, or code ported from C++/COM that uses 2D arrays, passing it to a generic ICollection.CopyTo API.","solutions":["Use a single-dimensional array as the destination (new KeyValuePair<ushort,double>[Count])","Flatten multidimensional data into a 1D array before calling CopyTo","Check array.Rank == 1 before calling CopyTo"],"exampleFix":"// before\nvar metrics = new KeyValuePair<ushort, double>[5, 5];\nglyphTypeface.AdvanceMetrics.CopyTo(metrics, 0);\n// after\nvar metrics = new KeyValuePair<ushort, double>[glyphTypeface.AdvanceMetrics.Count];\nglyphTypeface.AdvanceMetrics.CopyTo(metrics, 0);","handlingStrategy":"validation","validationCode":"if (dest == null) throw new ArgumentNullException(nameof(dest));\nif (dest.Rank != 1) throw new ArgumentException(\"Destination array must be single-dimensional\", nameof(dest));\nif (arrayIndex < 0 || arrayIndex >= dest.Length) throw new ArgumentOutOfRangeException(nameof(arrayIndex));","typeGuard":"bool CanCopyTo<T>(T[] array) => array != null && array.Rank == 1;","tryCatchPattern":null,"preventionTips":["Always allocate 1D arrays for ICollection.CopyTo destinations","Check array.Rank before generic copy helpers"],"tags":["wpf","glyphs","argument-exception","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"}