-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Closed
Description
Mdsn docs states that enumerator is valid until collection is changed. However it is not true on remove for net5.0, net6.0.
Source code for HashSet<T>.Remove(T item) doesn't increment _version. Is this behavior intended?
Code sample below throws on net472, netcoreapp3.1, but works on net5.0, net6.0.
public class Data
{
public int Id { get; set; }
public bool ToRemove { get; set; }
}
static void Main(string[] args)
{
var set = new HashSet<Data>();
for (var i = 0; i < 500; i++)
{
var removeFlag = i % 2 == 1;
set.Add(new Data { Id = i, ToRemove = removeFlag });
}
var toRemove = set.Where(d => d.ToRemove);
foreach(var d in toRemove)
{
_ = set.Remove(d);
}
Console.WriteLine("Success");
Console.WriteLine(set.Count);
foreach (var d in set)
Console.WriteLine(d.Id);
}Environment:
.NET SDK (reflecting any global.json):
Version: 6.0.402
Commit: 6862418796
Runtime Environment:
OS Name: Windows
OS Version: 10.0.19045
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\6.0.402\
global.json file:
Not found
Host:
Version: 6.0.11
Architecture: x64
Commit: 943474ca16
.NET SDKs installed:
3.1.425 [C:\Program Files\dotnet\sdk]
5.0.413 [C:\Program Files\dotnet\sdk]
6.0.402 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 3.1.31 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.31 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.31 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Metadata
Metadata
Assignees
Labels
No labels