{"record":{"id":"cec7ebe32c9d1e75","repo":"dotnet/wpf","slug":"sr-format-sr-collection-copyto-familytypefacecollection","errorCode":null,"errorMessage":"SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, \"index\", \"array\")","messagePattern":"SR\\.Format\\(SR\\.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, \"index\", \"array\"\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyTypefaceCollection.cs","lineNumber":376,"sourceCode":"            if (familyTypeface == null)\n                throw new ArgumentException(SR.Format(SR.CannotConvertType, obj.GetType(), typeof(FamilyTypeface)));\n\n            return familyTypeface;\n        }\n\n        private void CopyItems(Array array, int index)\n        {\n            ArgumentNullException.ThrowIfNull(array);\n\n            if (array.Rank != 1)\n                throw new ArgumentException(SR.Collection_CopyTo_ArrayCannotBeMultidimensional);\n\n            Type elementType = array.GetType().GetElementType();\n            if (!elementType.IsAssignableFrom(typeof(FamilyTypeface)))\n                throw new ArgumentException(SR.Format(SR.CannotConvertType, typeof(FamilyTypeface[]), elementType));\n\n            if (index >= array.Length)\n                throw new ArgumentException(SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, \"index\", \"array\"));\n\n            if (_count > array.Length - index)\n                throw new ArgumentException(SR.Format(SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength, index, \"array\"));\n\n            if (_count != 0)\n            {\n                InitializeItemsFromInnerList();\n                Array.Copy(_items, 0, array, index, _count);\n            }\n        }\n\n        private class Enumerator : IEnumerator<FamilyTypeface>, SC.IEnumerator\n        {\n            private FamilyTypefaceCollection _list;\n            private int _index;\n            private FamilyTypeface _current;\n\n            internal Enumerator(FamilyTypefaceCollection list)","sourceCodeStart":358,"sourceCodeEnd":394,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyTypefaceCollection.cs#L358-L394","documentation":"CopyTo checks that the starting index is within the destination array: if index >= array.Length there is no room for any element, so CopyItems throws ArgumentException with Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength.","triggerScenarios":"Calling CopyTo(array, index) with an index equal to or larger than array.Length — e.g. CopyTo(arr, arr.Length), or a computed offset that exceeds the array bounds.","commonSituations":"Appending multiple collections into one array where accumulated offsets exceed the array length, or copying into an empty array with a nonzero index.","solutions":["Ensure index < array.Length before calling CopyTo.","Size the destination array as collection.Count + index.","Use Array.Copy semantics or ToArray() when copying the whole collection from position 0."],"exampleFix":"// before\nvar dest = new FamilyTypeface[collection.Count];\ncollection.CopyTo(dest, collection.Count); // ArgumentException\n// after\nvar dest = new FamilyTypeface[collection.Count * 2];\ncollection.CopyTo(dest, collection.Count);","handlingStrategy":"validation","validationCode":"if (array == null || arrayIndex >= array.Length) throw new ArgumentException(\"index must be less than array.Length\");\nif (collection.Count > array.Length - arrayIndex) throw new ArgumentException(\"array too small\");","typeGuard":null,"tryCatchPattern":"try { collection.CopyTo(array, arrayIndex); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"IndexGreaterThanOrEqualToArrayLength\")) { /* adjust index or array size */ }","preventionTips":["Compute offsets before CopyTo and assert index < array.Length.","Size arrays as count + offset.","Prefer CopyTo at index 0 for full-collection copies."],"tags":["wpf","copyto","arrays","argument-out-of-range"],"backgroundTag":"argument-out-of-range","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"}