{"record":{"id":"7cffda21d37bd77e","repo":"dotnet/wpf","slug":"sr-format-sr-collection-copyto-7cffda","errorCode":null,"errorMessage":"SR.Format(SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength, index, \"array\")","messagePattern":"SR\\.Format\\(SR\\.Collection_CopyTo_NumberOfElementsExceedsArrayLength, index, \"array\"\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyTypefaceCollection.cs","lineNumber":379,"sourceCode":"            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)\n            {\n                _list = list;\n                _index = -1;","sourceCodeStart":361,"sourceCodeEnd":397,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyTypefaceCollection.cs#L361-L397","documentation":"CopyItems (invoked from FamilyTypefaceCollection.CopyTo) throws this ArgumentException when the destination array starting at 'index' is too small to hold all _count FamilyTypeface elements in the collection. It mirrors standard ICollection.CopyTo contract enforcement, throwing before Array.Copy runs so the destination array is not partially modified.","triggerScenarios":"Calling FamilyTypefaceCollection.CopyTo(array, index) where _count > array.Length - index, i.e. array has fewer than count+index elements (e.g. copying 5 typefaces into a 3-element array, or into a 10-element array at index 8).","commonSituations":"Sizing the target array from a stale or wrong count, reusing a buffer sized for a different collection, or passing an offset that leaves insufficient room.","solutions":["Size the destination array to collection.Count (e.g. new FamilyTypeface[ collection.Count ]) and pass index 0.","When using an offset, ensure array.Length >= collection.Count + index before calling CopyTo.","Or use LINQ ToArray()/ToList() instead of manual CopyTo."],"exampleFix":"// before\nvar arr = new FamilyTypeface[3];\ncollection.CopyTo(arr, 0);\n// after\nvar arr = new FamilyTypeface[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 1-D\");\nif (index >= array.Length) throw new ArgumentOutOfRangeException(nameof(index));\nif (collection.Count > array.Length - index)\n    throw new ArgumentException(\"array too small at index\");","typeGuard":null,"tryCatchPattern":"try { collection.CopyTo(array, index); }\ncatch (ArgumentException ex) { /* resize buffer and retry */ }","preventionTips":["Always allocate the destination array with collection.Count elements.","Never reuse buffers sized from a previous snapshot without re-checking Count.","Prefer ToArray()/ToList() over manual CopyTo."],"tags":["wpf","collections","argumentexception","copyto"],"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-21T21:30:21.729Z"}