This topic compares HashSet<(Of <(T>)>) operations and Language-Integrated Query (LINQ) set operations.
LINQ provides access to some set operations to any data source that implements the IEnumerable or IQueryable interfaces. This includes arrays and collections.
The set operations that LINQ provides are as follows:
Distinct
Union
Intersect
Except
For more information about LINQ, see Language-Integrated Query (LINQ). For more information about LINQ set operations, see Set Operations.
The primary difference between LINQ set operations and HashSet<(Of <(T>)>) operations is that the LINQ set operations always return a new IEnumerable<(Of <(T>)>) collection, while the HashSet<(Of <(T>)>) equivalent methods modify the current collection. HashSet<(Of <(T>)>) provides a larger and more robust collection of set operations. For example, HashSet<(Of <(T>)>) provides comparisons such as IsSubsetOf and IsSupersetOf.
Typically, if you must create a new set or if your application needs access only to the provided set operations, using LINQ set operations on any IEnumerable<(Of <(T>)>) collection or array will be sufficient. However, if your application requires access to additional set operations, or if it is not desirable or necessary to create a new collection, use the HashSet<(Of <(T>)>) class.
HashSet and LINQ Set Operations
The following table shows the HashSet<(Of <(T>)>) operations and their equivalent LINQ set operations.
Concepts
Reference
Other Resources