C# HashSet Methods

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().
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:
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 methodsPut items in, take them out, or empty the whole set. Add and Remove tell you with a bool whether anything changed.
| Method | Description | Tutorial |
|---|---|---|
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 methodsAsk whether an item is in the set, or get the exact stored copy of an item.
| Method | Description | Tutorial |
|---|---|---|
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 methodsUnion, intersection, difference, and symmetric difference. These change the set you call them on, in place.
| Method | Description | Tutorial |
|---|---|---|
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 methodsCompare a set with another collection. These only answer true or false and never change the set.
| Method | Description | Tutorial |
|---|---|---|
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 methodsReserve space before a big load, or give back unused space after a big clean-up.
| Method | Description | Tutorial |
|---|---|---|
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 methodsCopy the items into an array, or walk through them one by one.
| Method | Description | Tutorial |
|---|---|---|
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
Start with Add()
Put your first item into a set and see what happens when you add the same item twice.
12 people found this page helpful
