Repository navigation
Description
Description
On .NET 11.0.0, HashSet<T>.UnionWith(other) throws IndexOutOfRangeException when other is a HashSet<T> with the same comparer and the target set has Count == 0 because its elements were removed (Remove / ExceptWith), not because it is new or was cleared with Clear().
This looks like a regression from #122952 ("Optimize HashSet.UnionWith to copy data from another HashSet when empty"). UnionWith now calls ConstructFrom(other) when Count == 0, but ConstructFrom appears to assume a brand-new set: when it takes the path that re-adds the elements one by one, the target still carries its old free-list state, and AddIfNotPresent indexes out of bounds.
Reproduction Steps
var target = new HashSet<string>(StringComparer.Ordinal) { "s0" };
target.Remove("s0"); // Count == 0, but not a fresh set
var source = new HashSet<string>(StringComparer.Ordinal);
for (int i = 0; i < 8; i++) source.Add("k" + i);
for (int i = 0; i < 4; i++) source.Remove("k" + i); // sparse source
target.UnionWith(source); // throws on .NET 11.0.0Expected behavior
target contains k4..k7, as on .NET 10.
Actual behavior
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.Collections.Generic.HashSet`1.AddIfNotPresent(T value, Int32& location)
at System.Collections.Generic.HashSet`1.ConstructFrom(HashSet`1 source)
at System.Collections.Generic.HashSet`1.UnionWith(IEnumerable`1 other)
Regression?
Yes. The same program prints k4,k5,k6,k7 on .NET 10.0.14 and throws on .NET 11.0.0 (SDK 11.0.100).
Known Workarounds
Add the elements in a loop (foreach (var x in source) target.Add(x);), or pass a non-HashSet sequence to UnionWith.
Configuration
- .NET 11.0.0 (SDK 11.0.100), macOS, Arm64.
- Not specific to the element type or comparer:
HashSet<int>with the default comparer fails the same way.
Other information
With the same source, a newly created target and a target emptied by Clear() both work; only a target emptied by removals fails.
The source does not need removals of its own. A source created with a larger capacity fails too:
var target = new HashSet<string>(StringComparer.Ordinal) { "s0" };
target.Remove("s0");
var source = new HashSet<string>(100, StringComparer.Ordinal) { "a" };
target.UnionWith(source); // also throwsIn a randomized probe (200,000 shapes; the private fields read by reflection), the exception occurred exactly when all of these held:
target.Count == 0, andtarget._freeCount > 0(emptied by removals), andsource.Count > 0with the same effective comparer, andHashHelpers.ExpandPrime(source.Count + 1) < source._buckets.Length(the source is sparse, soConstructFromre-adds the elements instead of copying the arrays).
SymmetricExceptWith(HashSet) failed in the same shapes, presumably because it calls UnionWith when the set is empty. ExceptWith, IntersectWith, the copy constructor, and UnionWith with a non-HashSet argument did not fail in the same probe.
The tests added in #122952 seem to use only a newly created, empty destination, which would explain why this was not caught.
Activity
dotnet-policy-service commentedon Nov 18, 2026
Tagging subscribers to this area: @dotnet/area-system-collections
See info in area-owners.md if you want to be subscribed.