You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Incorrect exception message and type produced by CopyTo method from ICollection and ICollection<> in Dictionary<TKey, TValue> #131782
The method ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[], int) from Dictionary<TKey, TValue> seems to produce an incorrect exception message in some situations, telling that a non-negative index is required when that is not exactly the error that was produced.
Reproduction Steps
using System.Collections.Generic;
Dictionary<int, int> actual = new();
KeyValuePair<int, int>[] array = new KeyValuePair<int, int>[0];
((ICollection<KeyValuePair<int, int>>)actual).CopyTo(array, array.Length + 1);
The same also happens if the dictionary isn't empty or the array has some length.
It also happens with non-generic ICollection method.
Expected behavior
System.ArgumentException: The number of elements in the source is greater than the available space from index to the end of the destination array. (Parameter 'index') (According to documentation)
Actual behavior
System.ArgumentOutOfRangeException: Non-negative number required. (Parameter 'index')
Regression?
No response
Known Workarounds
Add an early check in my code.
Configuration
Sharplab default produces the error.
I also found it in .NET 10, Windows 11 x64.
Other information
If tweaking the behaviour this is a breaking change, then maybe the documentation should be aligned with the current behaviour.
I checked exception message of a few other major dictionaries and here are the results.
ImmutableDictionary: System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values. (Parameter 'arrayIndex')'
ImmutableSortedDictionary: System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values. (Parameter 'arrayIndex')'
FrozenDictionary: System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values.'
I thought it might be better to just update the exception message rather than changing the exception type from ArgumentOutOfRangeException to ArgumentException.
I checked exception message of a few other major dictionaries and here are the results.
ImmutableDictionary: System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values. (Parameter 'arrayIndex')'
ImmutableSortedDictionary: System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values. (Parameter 'arrayIndex')'
FrozenDictionary: System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values.'
I thought it might be better to just update the exception message rather than changing the exception type from ArgumentOutOfRangeException to ArgumentException.
I see, my reasoning was based on the documentation of ICollection<T>.CopyTo(T[], int) which uses ArgumentException for that error and ArgumentOutOfRangeException for a negative index. Shouldn't it align with the interface it implements?
I agree that exception types should match the documentation as closely as possible. However, given how widely used Dictionary is, I think it's necessary to confirm with the area owners whether this breaking change is acceptable.
ArgumentException is the base type of the other exceptions there, so it still satisfies.
I am doubtful we can or should make a break here. At best we could just improve the exception message for this scenario (although it isn’t strictly incorrect, just confusing)
I agree we should just update the exception message. There's an existing ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessOrEqualException() that yields:
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than or equal to the size of the collection. (Parameter 'index')
That's pretty close but still leaves a little ambiguity regarding which collection's size is referenced.
Marking as
help wanted[up-for-grabs] Good issue for external contributors
. If a PR is accepted within the next couple weeks, we could backport into .NET 11.
Activity
dotnet-policy-service commentedon Aug 4, 2026
Tagging subscribers to this area: @dotnet/area-system-collections
See info in area-owners.md if you want to be subscribed.
prozolic commentedon Aug 4, 2026
I checked exception message of a few other major dictionaries and here are the results.
ImmutableDictionary:System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values. (Parameter 'arrayIndex')'ImmutableSortedDictionary:System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values. (Parameter 'arrayIndex')'FrozenDictionary:System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values.'I thought it might be better to just update the exception message rather than changing the exception type from
ArgumentOutOfRangeExceptiontoArgumentException.Enderlook commentedon Aug 5, 2026
I see, my reasoning was based on the documentation of
ICollection<T>.CopyTo(T[], int)which usesArgumentExceptionfor that error andArgumentOutOfRangeExceptionfor a negative index. Shouldn't it align with the interface it implements?prozolic commentedon Aug 6, 2026
I agree that exception types should match the documentation as closely as possible. However, given how widely used
Dictionaryis, I think it's necessary to confirm with the area owners whether this breaking change is acceptable.tannergooding commentedon Aug 7, 2026
ArgumentException is the base type of the other exceptions there, so it still satisfies.
I am doubtful we can or should make a break here. At best we could just improve the exception message for this scenario (although it isn’t strictly incorrect, just confusing)
jeffhandley commentedon Aug 17, 2026
I agree we should just update the exception message. There's an existing
ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessOrEqualException()that yields:That's pretty close but still leaves a little ambiguity regarding which collection's size is referenced.
Marking as help wanted[up-for-grabs] Good issue for external contributors
. If a PR is accepted within the next couple weeks, we could backport into .NET 11.
Fix confusing exception message in `Dictionary.CopyTo` when index exc…
[release/11.0] Fix confusing exception message in `Dictionary.CopyTo`…