VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/c-sharp-intersection-of-two-hashsets/

⇱ C# | Intersection of two HashSets - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C# | Intersection of two HashSets

Last Updated : 11 Jul, 2025
A HashSet is an unordered collection of the unique elements. It is found in System.Collections.Generic namespace. It is used in a situation where we want to prevent duplicates from being inserted in the collection. As far as performance is concerned, it is better in comparison to the list. HashSetis used to modify the current HashSet object to contain only elements that are present in that object and in the specified collection. Syntax:
mySet1.IntersectWith(mySet2)
Here mySet1 and mySet2 are the two HashSets. Exception: This method will give ArgumentNullException if the HashSet is null. Below given are some examples to understand the implementation in a better way: Example 1:
Output:
Elements in Set 1 :
0
2
4
6
8
Elements in Set 2 : 
1
3
5
7
9
Example 2:
Comment

Explore