{"record":{"id":"5ac1d1c359a7ebc8","repo":"dotnet/wpf","slug":"sr-collection-badrank-fonts","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/Fonts.cs","lineNumber":315,"sourceCode":"            }\n\n            public bool Contains(Typeface item)\n            {\n                foreach (Typeface t in this)\n                {\n                    if (t.Equals(item))\n                        return true;\n                }\n                return false;\n            }\n\n            public void CopyTo(Typeface[] 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                foreach (Typeface t in this)\n                {\n                    array[arrayIndex++] = t;\n                }\n            }\n\n            public int Count\n            {\n                get\n                {","sourceCodeStart":297,"sourceCodeEnd":333,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Fonts.cs#L297-L333","documentation":"TypefaceCollection.CopyTo (Typeface enumeration helper in Fonts.cs) throws this ArgumentException when the destination array has rank other than 1, i.e. it is multi-dimensional. ICollection<T>.CopyTo only supports single-dimensional, zero-based arrays.","triggerScenarios":"Calling CopyTo on a Typeface[] allocated as a rectangular/multi-dimensional array (e.g. new Typeface[2,2]) — or via the non-generic ICollection.CopyTo path with such an array.","commonSituations":"Reusing a 2D buffer for font lists, or legacy code with multi-dimensional arrays passed through reflection/late binding.","solutions":["Use a single-dimensional array: new Typeface[collection.Count].","Flatten multi-dimensional storage into a 1D array before copying.","Or use LINQ ToArray() to allocate the correct shape automatically."],"exampleFix":"// before\nvar arr = new Typeface[2, 2];\ncollection.CopyTo(arr, 0);\n// after\nvar arr = new Typeface[collection.Count];\ncollection.CopyTo(arr, 0);","handlingStrategy":"validation","validationCode":"if (array == null) throw new ArgumentNullException(nameof(array));\nif (array.Rank != 1) throw new ArgumentException(\"array must be single-dimensional\");\nif (arrayIndex < 0) throw new ArgumentOutOfRangeException(nameof(arrayIndex));","typeGuard":"bool IsCompatibleTarget(Array a) => a != null && a.Rank == 1;","tryCatchPattern":"try { collection.CopyTo(array, arrayIndex); }\ncatch (ArgumentException) { /* array was multi-dimensional; allocate 1-D and retry */ }","preventionTips":["Always pass single-dimensional arrays to ICollection.CopyTo.","Replace multi-dimensional buffers with jagged arrays or List<T>.","Prefer ToArray() to guarantee the correct array shape."],"tags":["wpf","collections","arrays","argumentexception"],"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"}