I believe that many of us find ourselves writing a code that checks if object already exists in collection before we add it. The purpose of this check is creating a collection which holds unique objects, or simply a math set.
A HashSet is a collection that contains no duplicate elements, with no particular order. It was introduced in .NET framework 3.5 and provides high-performance set operations.
Since this object was modeled as mathematical set, it supports common set operations like Union and Intersection.
A HashSet has Add method which returns a boolean result indicating whether an element was added to collection or not (in case it already exists).
HashSet Using Example
HashSet<int> set1 = new HashSet<int>();
set1.Add(1); //won't be added
HashSet<int> set2 = new HashSet<int>();
//Produces an united set by using Union method.
HashSet<int> unionSet = new HashSet<int>(set1.Union(set2));
//UnionWith - modifies set1 itself.
//Produces an intesected set by using Intersect method.
HashSet<int> interectSet = new HashSet<int>(set1.Intersect(set2));
//IntersectWith - modifies set1 itself.
You can see the complete HashSet documentation here.