Skip to content

Fix ImmutableDictionary ArgumentException docs: clarify duplicate-key-same-value is a no-op#12420

Merged
gewarren merged 2 commits intomainfrom
copilot/fix-immutabledictionary-add-doc
Mar 19, 2026
Merged

Fix ImmutableDictionary ArgumentException docs: clarify duplicate-key-same-value is a no-op#12420
gewarren merged 2 commits intomainfrom
copilot/fix-immutabledictionary-add-doc

Conversation

Copy link
Contributor

Copilot AI commented Mar 19, 2026

ImmutableDictionary uses KeyCollisionBehavior.ThrowIfValueDifferent, meaning adding a duplicate key with an equal value is silently ignored — but the existing ArgumentException docs implied an exception is thrown for any duplicate key.

Changes

  • **ImmutableDictionary2.xml** — Fixed IDictionary<TKey,TValue>.Addexplicit interface impl:"same key already exists""same key but a different value already exists"`
  • **ImmutableDictionary2+Builder.xml** — Same fix for Builder.Add`
  • ImmutableDictionary.xml — Added missing ArgumentException docs to all 8 ToImmutableDictionary enumerable-source overloads (key/value selector variants included); the Builder-based overload is excluded since a builder can't hold duplicate keys

The public ImmutableDictionary<TKey,TValue>.Add already had the correct wording ("but has a different value") and was left unchanged.

Original prompt

This section details on the original issue you should resolve

<issue_title>ImmutableDictionary.Add ArgumentException doc should clarify duplicate-key-with-same-value behavior</issue_title>
<issue_description>### Description

The XML doc for ImmutableDictionary<TKey, TValue>.Add, ImmutableDictionary<TKey, TValue>.Builder.Add, and ToImmutableDictionary states:

<exception cref="T:System.ArgumentException">An element with the same key already exists in the dictionary.</exception>

This implies the exception is thrown whenever a duplicate key is added, regardless of value. However, the actual behavior (by design) is that the exception is only thrown when the key exists with a different value. Adding a duplicate key with an equal value is silently treated as a no-op.

This was confirmed by inspecting the source code: the implementation uses KeyCollisionBehavior.ThrowIfValueDifferent (not ThrowAlways). See dotnet/runtime#125652 for the full analysis.

Affected APIs

Suggested fix

Update the ArgumentException doc to something like:

An element with the same key but a different value already exists in the dictionary.

Or add a Remarks note clarifying that adding a key-value pair where both the key and value match an existing entry is treated as a no-op and does not throw.</issue_description>

Comments on the Issue (you are @copilot in this section)


📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.

…-same-value behavior

Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix ImmutableDictionary.Add ArgumentException documentation Fix ImmutableDictionary ArgumentException docs: clarify duplicate-key-same-value is a no-op Mar 19, 2026
Copilot AI requested a review from gewarren March 19, 2026 03:17
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-collections

@gewarren gewarren marked this pull request as ready for review March 19, 2026 16:11
@gewarren gewarren requested a review from a team as a code owner March 19, 2026 16:11
Copilot AI review requested due to automatic review settings March 19, 2026 16:11
@gewarren gewarren merged commit f2616dd into main Mar 19, 2026
6 checks passed
@gewarren gewarren deleted the copilot/fix-immutabledictionary-add-doc branch March 19, 2026 16:11
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the API reference XML docs for System.Collections.Immutable.ImmutableDictionary to accurately describe when an ArgumentException is thrown on duplicate keys, aligning the documentation with the implemented “throw only if the existing value differs” behavior.

Changes:

  • Updated ArgumentException text for IDictionary<TKey,TValue>.Add (explicit impl) and ImmutableDictionary<TKey,TValue>.Builder.Add to clarify it throws only when the existing value is different.
  • Added missing ArgumentException documentation to the enumerable-source ToImmutableDictionary overloads to match the same “different value” collision behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
xml/System.Collections.Immutable/ImmutableDictionary`2+Builder.xml Clarifies duplicate-key exception wording for Builder.Add.
xml/System.Collections.Immutable/ImmutableDictionary`2.xml Clarifies duplicate-key exception wording for the explicit IDictionary<TKey,TValue>.Add implementation.
xml/System.Collections.Immutable/ImmutableDictionary.xml Adds consistent ArgumentException docs to enumerable-source ToImmutableDictionary overloads.

You can also share your feedback on Copilot code review. Take the survey.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ImmutableDictionary.Add ArgumentException doc should clarify duplicate-key-with-same-value behavior

4 participants