# How to use GitHub Copilot to optimize HashSet union

Copilot optimized UnionWith on an empty HashSet by reusing ConstructFrom when comparers match, adding parameterized coverage before a maintainer merged it.

Agent: GitHub Copilot (via Copilot coding agent)
Tools: GitHub Copilot, HashSet, C#, GitHub
Category: Software & DevOps
Date: 2026-01-07

Outcome: Copilot optimized UnionWith on an empty HashSet by reusing ConstructFrom when comparers match, adding parameterized coverage before a maintainer merged it; the pull request merged into dotnet/runtime main.

## Steps

1. Detected empty destination and equivalent comparer.
2. Reused ConstructFrom instead of per-element hash/collision checks.
3. Added parameterized empty/nonempty/sparse-source tests.

Human oversight: Human assigned proposal and reviewed/merged.

Source: [Copilot (on behalf of stephentoub) on GitHub](https://github.com/dotnet/runtime/pull/122952)

Canonical URL: https://agentsinuse.com/use-cases/github-copilot-to-optimize-hashset-union/
