C# | Number of elements in HashSet Last Updated : 12 Jul, 2023 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. You can use HashSet.Count Property to count the number of elements in a HashSet. Syntax: mySet.Count; Here mySet is the HashSet Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to get the number of // elements that are contained in HashSet 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); } // To get the number of // elements that are contained in HashSet Console.WriteLine(mySet.Count); } } Output5Example 2: CSHARP // C# code to get the number of // elements that are contained in HashSet 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>(); // To get the number of // elements that are contained in HashSet. // Note that, here the HashSet is empty Console.WriteLine(mySet.Count); } } Output0Using the AsEnumerable and Count methods AsEnumerable: The AsEnumerable method is used to convert a collection into an enumerable type, enabling the use of LINQ extension methods.Count: The Count method returns the number of elements in a collection, providing a convenient way to determine its size. C# using System; using System.Collections.Generic; using System.Linq; public class GFG { static public void Main() { // Create a HashSet HashSet<int> numbers = new HashSet<int>(); numbers.Add(1); numbers.Add(2); numbers.Add(3); // Get the count of elements using the Enumerable.Count() method int elementCount = numbers.AsEnumerable().Count(); // Display the result Console.WriteLine("Number of elements in the HashSet: " + elementCount); } } OutputNumber of elements in the HashSet: 3 In this code, we create a HashSet called numbers and add three integer values to it. Then, we use the AsEnumerable() method to convert the HashSet to an IEnumerable<int>, and the Count() method from the Enumerable class to get the count of elements in the HashSet. The main heading is printed, followed by the elements of the HashSet and the count of elements. Comment More info S Sahil_Bansall Follow Improve Article Tags : Misc C# CSharp-Generic-HashSet CSharp-Generic-Namespace Explore IntroductionC# Tutorial 4 min read Introduction to .NET Framework 6 min read C# .NET Framework (Basic Architecture and Component Stack) 6 min read C# Hello World 2 min read Common Language Runtime (CLR) in C# 4 min read FundamentalsC# Identifiers 2 min read Data Types in C# 6 min read C# Variables 4 min read C# Literals 5 min read Operators in C# 7 min read C# Keywords 5 min read Control StatementsC# Decision Making (if, if-else, if-else-if ladder, nested if, switch, nested switch) 5 min read C# Switch Statement 4 min read Loops in C# 4 min read C# Jump Statements (Break, Continue, Goto, Return and Throw) 4 min read OOP ConceptsC# Class and Objects 5 min read Constructors in C# 5 min read C# Inheritance 3 min read C# Encapsulation 4 min read C# Abstraction 4 min read MethodsMethods in C# 4 min read Method Overloading in C# 4 min read C# | Method Parameters 7 min read C# Method Overriding 9 min read Anonymous Method in C# 3 min read ArraysArrays in C# 6 min read Jagged Arrays in C# 4 min read Array Class in C# 5 min read How to Sort an Array in C# | Array.Sort() Method Set - 1 8 min read How to find the rank of an array in C# 2 min read ArrayListArrayList in C# 6 min read C# ArrayList Class 7 min read C# | Array vs ArrayList 2 min read StringStrings in C# 6 min read C# Verbatim String Literal - @ 5 min read C# String Class 9 min read C# StringBuilder 4 min read C# String vs StringBuilder 3 min read TupleC# Tuple 7 min read C# Tuple Class 3 min read C# ValueTuple 7 min read C# ValueTuple Struct 4 min read IndexersC# Indexers 4 min read C# Multidimensional Indexers 5 min read C# - Overloading of Indexers 3 min read Like