{"record":{"id":"e7d6d2d5784d0f60","repo":"dotnet/wpf","slug":"sr-format-sr-collection-copyto-e7d6d2","errorCode":null,"errorMessage":"SR.Format(SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength, \"arrayIndex\", \"array\")","messagePattern":"SR\\.Format\\(SR\\.Collection_CopyTo_NumberOfElementsExceedsArrayLength, \"arrayIndex\", \"array\"\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Localizer/BamlLocalizationDictionary.cs","lineNumber":305,"sourceCode":"        }\n\n        /// <summary>\n        ///     Copies the dictionary's elements to a one-dimensional \n        ///     Array instance at the specified index.\n        /// </summary>\n        public void CopyTo(DictionaryEntry[] array, int arrayIndex)\n        {\n            ArgumentNullException.ThrowIfNull(array);\n            ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);\n\n            if (arrayIndex >= array.Length)\n            {\n                throw new ArgumentException(SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, \"arrayIndex\", \"array\"), nameof(arrayIndex));\n            }\n\n            if (Count > (array.Length - arrayIndex))\n            {\n                throw new ArgumentException(SR.Format(SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength, \"arrayIndex\", \"array\"));\n            }\n\n            foreach (KeyValuePair<BamlLocalizableResourceKey, BamlLocalizableResource> pair in _dictionary)\n            {\n                DictionaryEntry entry = new(pair.Key, pair.Value);\n                array[arrayIndex++] = entry;\n            }\n        }\n\n        #region interface ICollection, IEnumerable, IDictionary\n        //------------------------------\n        // interface functions\n        //------------------------------      \n\n        bool IDictionary.Contains(object key)\n        {\n            ArgumentNullException.ThrowIfNull(key);\n            return ((IDictionary)_dictionary).Contains(key);","sourceCodeStart":287,"sourceCodeEnd":323,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Localizer/BamlLocalizationDictionary.cs#L287-L323","documentation":"CopyTo throws this ArgumentException when the dictionary has more elements than fit in the destination array starting at arrayIndex, i.e. Count > array.Length - arrayIndex. The full set of entries could not be copied without overflowing the array, so the library refuses the operation up front.","triggerScenarios":"Calling BamlLocalizationDictionary.CopyTo with an array whose usable space (array.Length - arrayIndex) is smaller than the dictionary's Count, e.g. CopyTo(new DictionaryEntry[2], 0) when the dictionary holds 5 localizable resources.","commonSituations":"Sizing the destination array from a stale or cached count after the dictionary changed, allocating a buffer for a subset but copying everything, or passing a large arrayIndex into a barely-large-enough array.","solutions":["Size the destination array to exactly dict.Count (or more) before copying","Account for arrayIndex in the size: array.Length must be >= Count + arrayIndex","Re-read Count immediately before CopyTo if the dictionary can change","Use a List<DictionaryEntry> instead if a fixed buffer is not required"],"exampleFix":"// before\nvar entries = new DictionaryEntry[2];\ndict.CopyTo(entries, 0); // Count > array.Length\n// after\nvar entries = new DictionaryEntry[dict.Count];\ndict.CopyTo(entries, 0);","handlingStrategy":"validation","validationCode":"if (dict.Count > array.Length - arrayIndex)\n    throw new ArgumentException($\"Destination too small: need {dict.Count} slots from index {arrayIndex}, array has {array.Length}.\");","typeGuard":"bool FitsDestination<T>(T[] array, int arrayIndex, int count) => array != null && arrayIndex >= 0 && array.Length - arrayIndex >= count;","tryCatchPattern":null,"preventionTips":["Size destination arrays from dict.Count at the time of the copy, not earlier","Factor arrayIndex into the required size: length >= Count + arrayIndex","Prefer LINQ ToDictionaryEntry list building over fixed buffers when sizes vary","Beware stale counts when the dictionary is modified between sizing and copying"],"tags":["wpf","argument-exception","copyto","collections"],"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"}