Skip to content

Fix SortedSet.ExceptWith - #136671

Open
Hatr828 wants to merge 1 commit into
dotnet:mainfrom
Hatr828:fix-sortedset-view-exceptwith
Open

Hatr828 wants to merge 1 commit into
dotnet:mainfrom
Hatr828:fix-sortedset-view-exceptwith

Conversation

@Hatr828

@Hatr828 Hatr828 commented Oct 30, 2026

Copy link
Copy Markdown

SortedSet.ExceptWith can return without removing any elements when called on a view returned by GetViewBetween.
For example:

var set = new SortedSet<int> { 1, 2, 3, 4 };
var view = set.GetViewBetween(2, 3);

view.ExceptWith(view);

// Expected: view is empty;
// Actual: view contains { 2, 3 }.

The method checks the count field directly, but subset views calculate their count lazily. A newly created view can therefore have count == 0 even when it contains elements.

Added regression tests. Fixed the bug by changing count to Count.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 30, 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.

This branch has not been deployed

No deployments
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.

1 participant