Skip to content

HashSet<T>.UnionWith throws IndexOutOfRangeException when the set was emptied by removals (.NET 11 regression) #137834

Open

Description

@AdamEssenmacher

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.0

Expected 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 throws

In a randomized probe (200,000 shapes; the private fields read by reflection), the exception occurred exactly when all of these held:

  • target.Count == 0, and
  • target._freeCount > 0 (emptied by removals), and
  • source.Count > 0 with the same effective comparer, and
  • HashHelpers.ExpandPrime(source.Count + 1) < source._buckets.Length (the source is sparse, so ConstructFrom re-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

  1. added
    untriaged
    on Nov 18, 2026
  2. dotnet-policy-service commented on Nov 18, 2026

    @dotnet-policy-service
    Contributor

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Collectionsuntriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions