C# HashSet Methods

Beginner
5 min read
Updated: Nov 2026
20 method guides
Method index

About This Index

This page is the C# HashSet Methods index. Each row links to a full guide with syntax, parameters, return value, three examples, and FAQs. Examples use small fixed data such as HashSet<int> and HashSet<string>. Because a HashSet has no fixed order, the examples print counts, true / false answers, or sorted items, so the output you see is the output you get.

Learn what a HashSet is for on /c-sharp/hashset. Use this page when you need a specific API such as Add(), Contains(), or UnionWith().

Unique items, no promised order.

A HashSet stores each item once. Add returns false for a repeat. When you loop with foreach, do not depend on the order of items; sort them first when you need a fixed order.

How to Call a HashSet Method

Every HashSet method is an instance method, so you call it on your set variable:

C#
HashSet<int> numbers = new HashSet<int>();
bool added = numbers.Add(10);                  // add one item (false if it was already there)
bool has = numbers.Contains(10);               // look it up
numbers.UnionWith(new int[] { 20, 30 });       // add many items at once
bool same = numbers.SetEquals(new int[] { 10, 20, 30 });  // compare with another collection
bool gone = numbers.Remove(10);                // remove one item

C# HashSet Methods Table

All method guides in this C# tutorial series (20 tutorials), grouped by purpose.

Add / remove

4 methods

Put items in, take them out, or empty the whole set. Add and Remove tell you with a bool whether anything changed.

MethodDescriptionTutorial
Add()Add one item to a HashSet; returns true if it was new and false if it was already there.Try it
Remove()Remove one item from a HashSet; returns true if it was there and false if it was not.Try it
RemoveWhere()Remove every item that matches a rule; returns how many items were removed.Try it
Clear()Remove every item from a HashSet in one call; Count becomes 0.Try it

Look up

2 methods

Ask whether an item is in the set, or get the exact stored copy of an item.

MethodDescriptionTutorial
Contains()Check whether an item is in a HashSet; very fast, returns true or false.Try it
TryGetValue()Look up an item and get back the exact copy stored in the HashSet, if there is one.Try it

Set operations (change the set)

4 methods

Union, intersection, difference, and symmetric difference. These change the set you call them on, in place.

MethodDescriptionTutorial
UnionWith()Add every item of another collection into the HashSet (union); the set is changed in place.Try it
IntersectWith()Keep only the items that are also in another collection (intersection); the set is changed in place.Try it
ExceptWith()Remove every item that appears in another collection (set difference); the set is changed in place.Try it
SymmetricExceptWith()Keep items that are in only one of the two collections (symmetric difference); the set is changed in place.Try it

Set comparisons (return bool)

6 methods

Compare a set with another collection. These only answer true or false and never change the set.

MethodDescriptionTutorial
IsSubsetOf()Check whether every item of the HashSet is also in another collection; returns true or false.Try it
IsProperSubsetOf()Check whether the HashSet fits inside another collection that also has at least one more item.Try it
IsSupersetOf()Check whether the HashSet contains every item of another collection; returns true or false.Try it
IsProperSupersetOf()Check whether the HashSet contains every item of another collection and has at least one more.Try it
Overlaps()Check whether the HashSet shares at least one item with another collection; returns true or false.Try it
SetEquals()Check whether the HashSet and another collection hold exactly the same distinct items, in any order.Try it

Capacity

2 methods

Reserve space before a big load, or give back unused space after a big clean-up.

MethodDescriptionTutorial
EnsureCapacity()Pre-size the HashSet so it can hold at least a number of items without growing.Try it
TrimExcess()Give back unused space after a HashSet shrinks; the items stay the same.Try it

Copy / iterate

2 methods

Copy the items into an array, or walk through them one by one.

MethodDescriptionTutorial
CopyTo()Copy the items of a HashSet into an array; the order of the copied items is not promised.Try it
GetEnumerator()Get an enumerator to walk through a HashSet; foreach calls it for you.Try it

Frequently Asked Questions

They are the methods on System.Collections.Generic.HashSet<T>. They add and remove items, look items up, combine or compare sets, manage capacity, and copy or loop through the items.
This index lists 20 guides: Add, Remove, RemoveWhere, Clear, Contains, TryGetValue, UnionWith, IntersectWith, ExceptWith, SymmetricExceptWith, IsSubsetOf, IsProperSubsetOf, IsSupersetOf, IsProperSupersetOf, Overlaps, SetEquals, EnsureCapacity, TrimExcess, CopyTo, and GetEnumerator.
Add, Remove, RemoveWhere, Clear, UnionWith, IntersectWith, ExceptWith, and SymmetricExceptWith change the set in place. The Is... methods, Overlaps, SetEquals, and Contains only return an answer and leave the set alone.
No. A HashSet does not promise any order, so the order you see in a foreach loop can differ from the order you added items. Sort the items first if you need a fixed order.
Read the C# HashSet hub first. Then open Add(), Contains(), UnionWith(), and IntersectWith() from the tables below.

Start with Add()

Put your first item into a set and see what happens when you add the same item twice.

Add() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful