{"record":{"id":"6a836b965b07711d","repo":"dotnet/wpf","slug":"sr-format-sr-collection-copyto-charactermetricsdictionary","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/CharacterMetricsDictionary.cs","lineNumber":178,"sourceCode":"\n        bool SC.ICollection.IsSynchronized\n        {\n            get { return false; }\n        }\n\n        object SC.ICollection.SyncRoot\n        {\n            get { return this; }\n        }\n\n        void SC.ICollection.CopyTo(Array array, int index)\n        {\n            ArgumentNullException.ThrowIfNull(array);\n\n            ArgumentOutOfRangeException.ThrowIfNegative(index);\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            SC.DictionaryEntry[] typedArray = array as SC.DictionaryEntry[];\n            if (typedArray != null)\n            {\n                // it's an array of the exact type\n                foreach (KeyValuePair<int, CharacterMetrics> item in this)\n                {\n                    typedArray[index++] = new SC.DictionaryEntry(item.Key, item.Value);\n                }\n            }\n            else\n            {\n                // it's an array of some other type, e.g., object[]; make sure it's one dimensional\n                if (array.Rank != 1)\n                    throw new ArgumentException(SR.Collection_CopyTo_ArrayCannotBeMultidimensional);","sourceCodeStart":160,"sourceCodeEnd":196,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs#L160-L196","documentation":"The ICollection.CopyTo(Array array, int index) overload of CharacterMetricsDictionary validates that index is within the destination array: if index >= array.Length there is no room for even one element, so an ArgumentException with Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength is thrown.","triggerScenarios":"Calling ((ICollection)dictionary).CopyTo(array, index) with index >= array.Length on an Array (including DictionaryEntry[] or object[] targets).","commonSituations":"Interop with non-generic ICollection consumers passing a start offset computed elsewhere; copying into the tail of a shared buffer with a wrong offset; empty array with index 0.","solutions":["Ensure 0 <= index < array.Length.","Ensure array.Length >= index + dictionary.Count (the next check enforces room for all elements).","Guard with `if (index < array.Length)` or copy into a right-sized array at index 0."],"exampleFix":"// before\n((ICollection)dict).CopyTo(entries, entries.Length);\n// after\n((ICollection)dict).CopyTo(entries, 0);","handlingStrategy":"validation","validationCode":"if (index < 0 || index >= array.Length)\n    throw new ArgumentOutOfRangeException(nameof(index));","typeGuard":null,"tryCatchPattern":"try { ((ICollection)dict).CopyTo(array, index); }\ncatch (ArgumentException ex) { /* correct index or array and retry */ }","preventionTips":["Validate offsets passed into non-generic ICollection APIs.","Copy into freshly allocated arrays at index 0 when possible.","Remember Count elements must also fit after the index."],"tags":["wpf","icollection","copyto","argument-exception"],"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"}