dotnet/wpf · error
SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArra…
Error message
SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, "arrayIndex", "array")
What it means
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.
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);
Example fix
// before var entries = new DictionaryEntry[dict.Count]; dict.CopyTo(entries, entries.Length); // arrayIndex >= array.Length // after var entries = new DictionaryEntry[dict.Count]; dict.CopyTo(entries, 0);
Defensive patterns
Strategy: validation
Validate before calling
if (array == null) throw new ArgumentNullException(nameof(array));
if (arrayIndex < 0) throw new ArgumentOutOfRangeException(nameof(arrayIndex));
if (arrayIndex >= array.Length) throw new ArgumentException("arrayIndex must be < array.Length", nameof(arrayIndex));
if (dict.Count > array.Length - arrayIndex) throw new ArgumentException("Destination array too small."); Type guard
bool CanCopyTo(System.Array array, int arrayIndex, int count) => array != null && array.Rank == 1 && arrayIndex >= 0 && arrayIndex < array.Length && count <= array.Length - arrayIndex;
Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Collection_CopyTo_ArrayCannotBeMultidimensional
- Collection_CopyTo_ArrayCannotBeMultidimensional
- Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength
- Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength
- Collection_CopyTo_NumberOfElementsExceedsArrayLength
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/96801531b012e3de.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Localizer/BamlLocalizationDictionary.cs:300
/// </summary>
/// <value>number of localizable resources</value>
public int Count
{
get => _dictionary.Count;
}
/// <summary>
/// Copies the dictionary's elements to a one-dimensional
/// Array instance at the specified index.
/// </summary>
public void CopyTo(DictionaryEntry[] array, int arrayIndex)
{
ArgumentNullException.ThrowIfNull(array);
ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);
if (arrayIndex >= array.Length)
{
throw new ArgumentException(SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, "arrayIndex", "array"), nameof(arrayIndex));
}
if (Count > (array.Length - arrayIndex))
{
throw new ArgumentException(SR.Format(SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength, "arrayIndex", "array"));
}
foreach (KeyValuePair<BamlLocalizableResourceKey, BamlLocalizableResource> pair in _dictionary)
{
DictionaryEntry entry = new(pair.Key, pair.Value);
array[arrayIndex++] = entry;
}
}
#region interface ICollection, IEnumerable, IDictionary
//------------------------------
// interface functions
//------------------------------ View on GitHub (pinned to 81131a70a4)