C# | Check if a HashSet contains the specified element Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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. HashSet.Contains(T) Method is used to check whether a HashSet object contains the specified element. Syntax: mySet.Contains(T item); Here, mySet is the name of the HashSet and item is the required element to locate in the HashSet object. Return Type: This method returns true if the HashSet object contains the specified element; otherwise, false. Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to check if a HashSet // contains the specified element using System; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating a HashSet of strings HashSet<string> mySet = new HashSet<string>(); // Inserting elements in HashSet mySet.Add("DS"); mySet.Add("C++"); mySet.Add("Java"); mySet.Add("JavaScript"); // Check if a HashSet contains // the specified element if (mySet.Contains("Java")) Console.WriteLine("Required Element is present"); else Console.WriteLine("Required Element is not present"); } } Output: Required Element is present Example 2: CSHARP // C# code to check if a HashSet // contains the specified element using System; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating a HashSet of integers HashSet<int> mySet = new HashSet<int>(); // Inserting elements in HashSet for (int i = 0; i < 5; i++) { mySet.Add(i * 2); } // Check if a HashSet contains // the specified element if (mySet.Contains(5)) Console.WriteLine("Required Element is present"); else Console.WriteLine("Required Element is not present"); } } Output: Required Element is not present Reference: https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1.contains?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Check if the SortedSet contains a specific element S Sahil_Bansall Follow Improve Article Tags : Misc C# CSharp-method CSharp-Generic-HashSet CSharp-Generic-Namespace +1 More Practice Tags : Misc Similar Reads C# | Check if the SortedSet contains a specific element SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.Contains(T) Method is used to check if a SortedSet contains a specific element or not. Properties: In C#, SortedSet class can be used to store, remov 2 min read C# | Check if HashSet and the specified collection contain the same elements Here's an example code that demonstrates how to use the Overlaps method to check if a HashSet and a specified collection share common elements in C#: C# using System; using System.Collections.Generic; class Program { static void Main(string[] args) { // Create a HashSet and a List HashSet<int> 3 min read C# | How to check whether a List contains a specified element List<T>.Contains(T) Method is used to check whether an element is in the List<T> or not. Properties of List: It is different from the arrays. A list can be resized dynamically but arrays cannot.List class can accept null as a valid value for reference types and it also allows duplicate e 2 min read C# | Check if the Hashtable contains a specific Key The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.ContainsKey(Object) Method is used to check whether the Hashtable contains a specific key or not. Syntax: public v 2 min read C# | Check if the Hashtable contains a specific Value The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.ContainsValue(Object) Method is used to check whether the Hashtable contains a specific value or not. Syntax: publ 2 min read C# | Check if a HashSet and a specified collection share common elements In C#, you can use the SetEquals method to check if a HashSet and a specified collection contain the same elements. The SetEquals method returns true if the HashSet and the specified collection contain the same elements; otherwise, it returns false. Here is an example code that demonstrates how to u 3 min read Like