In this blog post we are going to learn about HashSet collection in C#. It is a cool collection available from C# 3.5. Like any other collection it can be used for representing a set of value. It is an optimized set collection. It helps eliminating duplicate string or elements in collection. Whenever an Item is added in collection it will check whether this items are already there in collection If not then only it will add items.
Let’s take an example for the same.
Let’s take an example for the same.
using System; using System.Collections.Generic; namespace CSharpHashSet { class Program { static void Main(string[] args) { HashSet<string> nameHashSet= new HashSet<string> { "Jalpesh", "Vishal", "Tushar", "Jalpesh" }; foreach (var item in nameHashSet) { Console.WriteLine(item); } Console.ReadKey(); } } }Now if you see the code carefully I have created HashSet of name(string type) with duplicate name like “Jalpesh” and then enumerate that collection with for loop and print items of collection with Console.WriteLine.