Skip to content

Incorrect exception message and type produced by CopyTo method from ICollection and ICollection<> in Dictionary<TKey, TValue> #131782

Closed

Description

@Enderlook

Description

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.

Activity

  1. added
    untriaged
    on Aug 4, 2026
  2. dotnet-policy-service commented on Aug 4, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-collections
    See info in area-owners.md if you want to be subscribed.

  3. prozolic commented on Aug 4, 2026

    @prozolic
    Contributor

    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.

  4. Enderlook commented on Aug 5, 2026

    @Enderlook
    Author

    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?

  5. prozolic commented on Aug 6, 2026

    @prozolic
    Contributor

    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.

  6. tannergooding commented on Aug 7, 2026

    @tannergooding
    Member

    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)

  7. jeffhandley commented on Aug 17, 2026

    @jeffhandley
    Member

    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.

  8. added this to the Future milestone on Aug 17, 2026
  9. added
    help wanted
    and removed
    untriaged
    on Aug 17, 2026
  10. added a commit that references this issue on Aug 20, 2026
    f5b4ce4
  11. locked and limited conversation to collaborators on Sep 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Collectionshelp wanted

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions