C# HashSet with Example

How to use HashSet in C#

What is HashSet in C#? C# has a list class to which duplicate items cannot be added. This class is called "HashSet" and is very useful in some cases. Since it can be used in the same way as the regular List class, it is well worth mastering.

Learn more:

Features of the HashSet class

  • Duplicate objects cannot be added. 
  • Duplicate checking is not required since all values must be unique.

How to use the HashSet class

To use the HashSet class, you must declare the following namespace:

using System.Collections.Generic;

As with List, assign the instantiated class to a variable.

Example:

HashSet<string> list = new HashSet<string>();

Or:

var list = new Hashset<string>();

Set elements into a HashSet

To set elements into a HashSet, you can write as follows:

  1. HashSet<string> fruits = new HashSet<string>
  2. {
  3. "Apple", "Melon", "Grape"
  4. };

The HashSet class is instantiated, and initial values are set.

Adding an element using the Add() method

You can add an element to a HashSet using the Add() method like this:

  1. HashSet<string> fruits = new HashSet<string>();
  2. fruits.Add("Grape");

Sample program:

  1. using System;
  2. using System.Collections.Generic;
  3. namespace CSharpExample
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. //Initialize
  10. HashSet<string> fruits = new HashSet<string>
  11. {
  12. "Apple", "Melon", "Grape"
  13. };
  14. Print(fruits);
  15. Console.WriteLine("*********************");
  16. //Add elements
  17. fruits.Add("Kiwi");
  18. fruits.Add("Orange");
  19. Print(fruits);
  20. }
  21. static void Print(HashSet<string> hashset)
  22. {
  23. foreach (string element in hashset)
  24. {
  25. Console.WriteLine("Element: {0}", element);
  26. }
  27. }
  28. }
  29. }

Output:

Element: Apple
Element: Melon       
Element: Grape       
*********************
Element: Apple       
Element: Melon       
Element: Grape       
Element: Kiwi        
Element: Orange 

Cannot set an element if the same element already exists

Example:

  1. HashSet<string> fruits = new HashSet<string>()
  2. {
  3. "Apple", "Melon", "Grape"
  4. };
  5. //Add elements
  6. fruits.Add("Kiwi");
  7. //Apple already exists
  8. fruits.Add("Apple");

Sample program:

  1. using System;
  2. using System.Collections.Generic;
  3. namespace CSharpExample
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. //Initialize
  10. HashSet<string> fruits = new HashSet<string>
  11. {
  12. "Apple", "Melon", "Grape"
  13. };
  14. Print(fruits);
  15. Console.WriteLine("*********************");
  16. //Add elements
  17. fruits.Add("Apple");
  18. Print(fruits);
  19. }
  20. static void Print(HashSet<string> hashset)
  21. {
  22. foreach (string element in hashset)
  23. {
  24. Console.WriteLine("Element: {0}", element);
  25. }
  26. }
  27. }
  28. }

Output:

Element: Apple
Element: Melon
Element: Grape
*********************
Element: Apple
Element: Melon
Element: Grape

Check for the presence of the specified element

To check for the presence of the specified element, use the Contains() method as the following example shows:

  1. HashSet<string> fruits = new HashSet<string>();
  2. //Add elements
  3. fruits.Add("Kiwi");
  4. fruits.Add("Grape");
  5. fruits.Add("Mango");
  6. fruits.Add("Apple");
  7. //If "Apple" is present
  8. if (fruits.Contains("Apple"))
  9. {
  10. //Code
  11. }

Sample program:

  1. using System;
  2. using System.Collections.Generic;
  3. namespace CSharpExample
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. //Initialize
  10. HashSet<string> fruits = new HashSet<string>
  11. {
  12. "Apple", "Melon", "Grape"
  13. };
  14. //Add elements
  15. fruits.Add("Orange");
  16. Print(fruits);
  17. Console.WriteLine("*********************");
  18. //Check if exists
  19. if (fruits.Contains("Orange"))
  20. {
  21. Console.WriteLine("\"Orange\" exists!");
  22. }
  23. else
  24. {
  25. Console.WriteLine("\"Orange\" does not exist!");
  26. }
  27. //
  28. if (fruits.Contains("Mango"))
  29. {
  30. Console.WriteLine("\"Mango\" exists!");
  31. }
  32. else
  33. {
  34. Console.WriteLine("\"Mango\" does not exist!");
  35. }
  36. }
  37. static void Print(HashSet<string> hashset)
  38. {
  39. foreach (string element in hashset)
  40. {
  41. Console.WriteLine("Element: {0}", element);
  42. }
  43. }
  44. }
  45. }

Output:

Element: Apple
Element: Melon
Element: Grape
Element: Orange
*********************
"Orange" exists!
"Mango" does not exist!

Deleting the specified element using the Remove method

Deletes an element by specifying the element. Here is an example:

  1. HashSet<string> fruits = new HashSet<string>();
  2. //Add elements
  3. fruits.Add("Grape");
  4. fruits.Add("Apple");
  5. fruits.Add("Mango");
  6. //Remove
  7. fruits.Remove("Apple");

Get the number of elements using the Count property

Get the number of elements using the Count property. Here is an example:

  1. HashSet<string> fruits = new HashSet<string>();
  2. //Add elements
  3. fruits.Add("Grape");
  4. fruits.Add("Apple");
  5. fruits.Add("Mango");
  6. fruits.Add("Kiwi");
  7. //Get the number of elements
  8. int count = fruits.Count;
  9. //Remove
  10. fruits.Remove("Apple");
  11. //Get the number of elements
  12. count = fruits.Count;

Sample program:

  1. using System;
  2. using System.Collections.Generic;
  3. namespace CSharpExample
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. //Initialize
  10. HashSet<string> fruits = new HashSet<string>
  11. {
  12. "Apple", "Melon", "Grape"
  13. };
  14. //Add elements
  15. fruits.Add("Pineapple");
  16. Print(fruits);
  17. Console.WriteLine("*********************");
  18. Console.WriteLine("The number of elements: {0}", fruits.Count);
  19. }
  20. static void Print(HashSet<string> hashset)
  21. {
  22. foreach (string element in hashset)
  23. {
  24. Console.WriteLine("Element: {0}", element);
  25. }
  26. }
  27. }
  28. }

Output:

Element: Apple
Element: Melon
Element: Grape
Element: Pineapple
*********************
The number of elements: 4

Delete all elements using the Clear() method

To delete all elements from a HashSet, use the Clear() method. Here is an example:

  1. HashSet<string> fruits = new HashSet<string>();
  2. //Add elements
  3. fruits.Add("Kiwi");
  4. fruits.Add("Grape");
  5. fruits.Add("Mango");
  6. fruits.Add("Apple");
  7. //Delete all elements
  8. fruits.Clear();

Summary

In this tutorial, you have learned how to use the C# HashSet class. If you need a list with unique elements, use HashSet.

All C# tutorials.


See also:
C# List Contains with Examples
C# typeof with Examples
C# TryParse (int, double, float) with Examples
C# Data Types
C# Variables