Skip to content

[release/11.0] Fix confusing exception message in Dictionary.CopyTo when index exceeds array length - #132571

Open
github-actions[bot] wants to merge 1 commit into
release/11.0from
backport/pr-132465-to-release/11.0
Open

[release/11.0] Fix confusing exception message in Dictionary.CopyTo when index exceeds array length#132571
github-actions[bot] wants to merge 1 commit into
release/11.0from
backport/pr-132465-to-release/11.0

Conversation

@github-actions

@github-actions github-actions Bot commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Backport of #132465 to release/11.0

/cc @jeffhandley @aw0lid

Customer Impact

  • 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
  • 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.

…eeds array length (#132465)

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.
@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.

@jeffhandley

Copy link
Copy Markdown
Member

/ba-g Failures are #132336 and a BadExit

@jeffhandley

Copy link
Copy Markdown
Member

@artl93 This is another customer-reported opportunity to improve an exception message that I recommend we consider for RC2.

@jeffhandley
jeffhandley requested a review from artl93 August 20, 2026 23:27
@jeffhandley jeffhandley added the Servicing-consider Issue for next servicing release review label Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Collections Servicing-consider Issue for next servicing release review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants