{"record":{"id":"96801531b012e3de","repo":"dotnet/wpf","slug":"sr-format-sr-collection-copyto-bamllocalizationdictionary","errorCode":null,"errorMessage":"SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, \"arrayIndex\", \"array\")","messagePattern":"SR\\.Format\\(SR\\.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, \"arrayIndex\", \"array\"\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Localizer/BamlLocalizationDictionary.cs","lineNumber":300,"sourceCode":"        /// </summary>\n        /// <value>number of localizable resources</value>\n        public int Count\n        {\n            get => _dictionary.Count;\n        }\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        //------------------------------      ","sourceCodeStart":282,"sourceCodeEnd":318,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Localizer/BamlLocalizationDictionary.cs#L282-L318","documentation":"CopyTo(Array array, int arrayIndex) on BamlLocalizationDictionary requires arrayIndex to be strictly less than the target array's length. When arrayIndex equals or exceeds array.Length there is no room to write even one element, so the dictionary throws ArgumentException with the Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength resource string. This is the standard ICollection.CopyTo contract used throughout .NET collections.","triggerScenarios":"Calling BamlLocalizationDictionary.CopyTo with an arrayIndex >= array.Length, e.g. CopyTo(entries, entries.Length) or CopyTo(entries, 5) on a 3-element array; note array itself must not be null (ArgumentNullException) and negative index is ArgumentOutOfRange.","commonSituations":"Off-by-one mistakes when computing a write offset into a larger buffer, passing the destination array length instead of an offset, reusing a cursor variable that was advanced past the array end, or copying into a zero-length array with any nonzero index.","solutions":["Ensure arrayIndex < array.Length before calling CopyTo","Use arrayIndex 0 to copy into a dedicated array sized Count","Validate the destination buffer is large enough (Count <= array.Length - arrayIndex)","Pass a fresh array: var entries = new DictionaryEntry[dict.Count]; dict.CopyTo(entries, 0);"],"exampleFix":"// before\nvar entries = new DictionaryEntry[dict.Count];\ndict.CopyTo(entries, entries.Length); // arrayIndex >= array.Length\n// after\nvar entries = new DictionaryEntry[dict.Count];\ndict.CopyTo(entries, 0);","handlingStrategy":"validation","validationCode":"if (array == null) throw new ArgumentNullException(nameof(array));\nif (arrayIndex < 0) throw new ArgumentOutOfRangeException(nameof(arrayIndex));\nif (arrayIndex >= array.Length) throw new ArgumentException(\"arrayIndex must be < array.Length\", nameof(arrayIndex));\nif (dict.Count > array.Length - arrayIndex) throw new ArgumentException(\"Destination array too small.\");","typeGuard":"bool CanCopyTo(System.Array array, int arrayIndex, int count) => array != null && array.Rank == 1 && arrayIndex >= 0 && arrayIndex < array.Length && count <= array.Length - arrayIndex;","tryCatchPattern":null,"preventionTips":["Always allocate the destination as new DictionaryEntry[dict.Count] and copy at index 0","Validate index bounds before any ICollection.CopyTo call","Remember arrayIndex must be strictly less than array.Length, not <=","Recompute Count right before the copy if the dictionary may mutate"],"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-21T21:30:21.729Z"}