User Id :    Password :      New Member   Forgot Password  
 
HashSet in C#
Description In this blog post we are going to learn about HashSet collection in C#.   No. of Views     2671
  Rating     0
Author Jalpesh Vadgama   Posted On     14 Aug 2014
Tags C#,Collection,C# : How To    

Sample Code   Download Code

It is a cool collection available from C# 3.5. Like any other collection it can be used for resenting 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.

 

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.

 

Now let’s run this application to see output.

 

 

If you see the output you can see that it has removed “Jalpesh” name duplication. So it was expected because HashSet automatically avoid duplication.

 

Advantages of HashSet:

  1. It’s automatically elements duplicate without giving any error.
  2. Searching is super fast as there is no duplication and also it uses hash base searching algorithms to find elements.
  3. You can also find items via indexes of items just like any other standard collections.

 

How to Add/Remove Element in HashSet:

There are multiple ways of initializing HashSet one of ways I have shown in above examples. But you can also use Add and Remove method to remove element in HashSet Collection like below.

 

nameHashSet.Add("Chirag");
nameHashSet.Remove("Tushar");

 

Now when you run example it will output as follows

 

 

You can also use any standard collection methods in HashSet like Clear, Contains etc. There is a complete list of methods available with HashSet collection is available at following MSDN link.

 

msdn.microsoft.com/en-us/library/bb359438(v=vs.110).aspx

 

You can find complete code for this blog post a following link.

 

 github.com/dotnetjalps/CSharpHashSet

 

That’s it. Hope you like it. Stay tuned for more!!

About Author

About Author I am jalpesh vadgama an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having more then 5 years of experience in .NET Technology.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible. Please also visit my blog at http://jalpesh.blogspot.com/ Jalpesh Vadgama
No Photo
 
Country India
Company N/A
Home Page http://jalpesh.blogspot.com/

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

   
Write your comment here.
Comment
Verification Code