Skip to content

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

Merged
jeffhandley merged 1 commit into
dotnet:mainfrom
aw0lid:fix/dictionary-copyto-exception-message
Aug 20, 2026
Merged

Fix confusing exception message in Dictionary.CopyTo when index exceeds array length#132465
jeffhandley merged 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.

@jeffhandley
jeffhandley merged commit f5b4ce4 into dotnet:main Aug 20, 2026
147 checks passed
@jeffhandley

Copy link
Copy Markdown
Member

/backport to release/11.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0 (link to workflow run)

jeffhandley pushed a commit that referenced this pull request Aug 21, 2026
… when index exceeds array length (#132571)

Backport of #132465 to release/11.0

/cc @jeffhandley @aw0lid

## Customer Impact

- [x] Customer reported
- [ ] Found internally

Customer-reported in #131782. A positive `index` beyond the destination
array length causes generic `Dictionary<TKey, TValue>.CopyTo` to throw
`ArgumentOutOfRangeException` with the misleading message "Non-negative
number required." This fix keeps the exception type and parameter while
using a clearer existing message. The separately reported non-generic
path is unchanged.

The change in #132465 was also a community contribution to improve the
message.

## Regression

- [ ] Yes
- [x] No

## Testing

Existing tests cover this invalid index and assert the exception type,
but not the exception message. No tests were added.

## Risk

Low. Only the message resource used for the exception is changed,
reusing a different existing resource with a clearer message.

Co-authored-by: Ahmed Waleed <ahmedwalidahmed.0@gmail.com>
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>

2 participants