Skip to content

Fix confusing exception message in Dictionary.CopyTo when index exceeds array length - #132465

Open
aw0lid wants to merge 1 commit into
dotnet:mainfrom
aw0lid:fix/dictionary-copyto-exception-message
Open

Fix confusing exception message in Dictionary.CopyTo when index exceeds array length#132465
aw0lid wants to merge 1 commit into
dotnet:mainfrom
aw0lid:fix/dictionary-copyto-exception-message

Conversation

@aw0lid

@aw0lid aw0lid commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Fixes #131782

Description

This PR improves the exception message produced by Dictionary<TKey, TValue>'s CopyTo methods when the provided index exceeds the target array's length.

Instead of throwing the misleading Non-negative number required message, it now utilizes ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessOrEqualException() to yield a clearer message while maintaining the existing ArgumentOutOfRangeException type.

Customer Impact

Prevents confusion when developers encounter unexpected index-related validation errors during collection copy operations.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Aug 18, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@aw0lid
aw0lid marked this pull request as ready for review August 18, 2026 13:50
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@aw0lid aw0lid closed this Aug 18, 2026
@aw0lid aw0lid reopened this Aug 18, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Collections community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant