
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Check if a HashSet contains the specified element in C#
To check if a HashSet contains the specified element, the code is as follows −
Example
using System; using System.Collections.Generic; public class Demo { public static void Main(){ HashSet<int> set1 = new HashSet<int>(); set1.Add(25); set1.Add(50); set1.Add(75); set1.Add(100); set1.Add(125); set1.Add(150); Console.WriteLine("Elements in HashSet1"); foreach(int val in set1){ Console.WriteLine(val); } HashSet<int> set2 = new HashSet<int>(); set2.Add(30); set2.Add(60); set2.Add(100); set2.Add(150); set2.Add(200); set2.Add(250); Console.WriteLine("Elements in HashSet2"); foreach(int val in set2){ Console.WriteLine(val); } Console.WriteLine("Do they share common elements? "+set1.Overlaps(set2)); Console.WriteLine("Does HashSet1 has element 60? "+set1.Contains(60)); Console.WriteLine("Does HashSet2 has element 60? "+set2.Contains(60)); } }
Output
This will produce the following output −
Elements in HashSet1 25 50 75 100 125 150 Elements in HashSet2 30 60 100 150 200 250 Do they share common elements? True Does HashSet1 has element 60? False Does HashSet2 has element 60? True
Example
Let us now see another example; using System; using System.Collections.Generic; public class Demo { public static void Main(){ HashSet<string> hashSet = new HashSet<string>(); hashSet.Add("Tim"); hashSet.Add("Jack"); hashSet.Add("Matt"); hashSet.Add("Steve"); hashSet.Add("David"); hashSet.Add("Kane"); hashSet.Add("Gary"); Console.WriteLine("Elements in HashSet"); foreach(string val in hashSet){ Console.WriteLine(val); } if (hashSet.Contains("Matt")) Console.WriteLine("The element Matt is in the HashSet"); else Console.WriteLine("The element Matt is not in the HashSet"); } }
Output
This will produce the following output −
Elements in HashSet Tim Jack Matt Steve David Kane Gary The element Matt is in the HashSet
- Related Articles
- Check if a HashSet and a specified collection share common element in C#
- Check if a Java HashSet Collection contains another Collection
- Check if a HashSet is a subset of the specified collection in C#
- Check if a HashSet is a superset of the specified collection in C#
- Remove the specified element from a HashSet in C#
- Check if a HashSet is a proper subset of the specified collection in C#
- Check if a HashSet is a proper superset of the specified collection in C#
- Check if HashSet and the specified collection contain the same elements in C#
- Remove specified element from HashSet in Java
- How to check whether a List contains a specified element in C#
- Java Program to check if a particular element exists in HashSet
- Check if the SortedSet contains a specific element in C#
- Check if SortedDictionary contains the specified key or not in C#
- Check if a Stack contains an element in C#
- Checking if a HashSet contains certain value in Java

Advertisements