Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hashset Remove is not throwing on enumeration #8177

Open
wrexbe opened this issue Jun 25, 2022 · 3 comments
Open

Hashset Remove is not throwing on enumeration #8177

wrexbe opened this issue Jun 25, 2022 · 3 comments
Labels
area-System.Collections doc-update Update outdated articles Pri3 Indicates issues/PRs that are low priority
Milestone

Comments

@wrexbe
Copy link

wrexbe commented Jun 25, 2022

Description

I found some code that was looping over a hashset, and removing items. Hashset is not throwing when you remove while enumerating like it used to.
It's convenient that it's like this, but it's different then how it used to work, and I'm not sure if it's safe.

The documentation says it should throw, so either the documentation needs to be updated, or the Hashset needs to be fixed.
https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1.getenumerator?view=net-6.0

Reproduction Steps

HashSet<int> set = new();
set.Add(1);
set.Add(2);

foreach (var x in set)
{
    set.Remove(x);
}

Expected behavior

Throws an invalid operation exception

Actual behavior

It works

Regression?

.Net 3.1 throws

Known Workarounds

Don't do it?

Configuration

No response

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jun 25, 2022
@ghost
Copy link

ghost commented Jun 25, 2022

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

Issue Details

Description

I found some code that was looping over a hashset, and removing. Hashset is not throwing when you remove while enumerating like it used to.
It's convenient that it's like this, but it's different then how it used to work, and I'm not sure if it's safe.

The documentation says it should throw, so either the documentation needs to be updated, or the Hashset needs to be fixed.
https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1.getenumerator?view=net-6.0

Reproduction Steps

HashSet<int> set = new();
set.Add(1);
set.Add(2);

foreach (var x in set)
{
    set.Remove(x);
}

Expected behavior

Throws an invalid operation exception

Actual behavior

It works

Regression?

.Net 3.1 throws

Known Workarounds

Don't do it?

Configuration

No response

Other information

No response

Author: wrexbe
Assignees: -
Labels:

area-System.Collections

Milestone: -

@stephentoub
Copy link
Member

Both HashSet and Dictionary have been improved to support removal during enumeration. The docs may just benefit from updating.

@theodorzoulias
Copy link
Contributor

Related: Remove version increment from Dictionary<K,V>.Remove overloads

I was not aware that it affects the HashSet<T> collection too though.

@eiriktsarpalis eiriktsarpalis removed the untriaged New issue has not been triaged by the area owner label Jun 27, 2022
@PRMerger13 PRMerger13 added the Pri3 Indicates issues/PRs that are low priority label Jun 27, 2022
@eiriktsarpalis eiriktsarpalis transferred this issue from dotnet/runtime Jun 27, 2022
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jun 27, 2022
@krwq krwq added the doc-update Update outdated articles label Jun 28, 2022
@eiriktsarpalis eiriktsarpalis removed the untriaged New issue has not been triaged by the area owner label Aug 2, 2022
@eiriktsarpalis eiriktsarpalis added this to the Backlog milestone Aug 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Collections doc-update Update outdated articles Pri3 Indicates issues/PRs that are low priority
Projects
None yet
Development

No branches or pull requests

6 participants