Add element to HashSet in C#

The HashSet in C# is a collection that stores unique elements without duplicates. To add elements to a HashSet, you use the Add() method, which returns a boolean value indicating whether the element was successfully added.

Syntax

Following is the syntax for adding an element to a HashSet −

bool result = hashSet.Add(element);

Return Value

The Add() returns −

  • true if the element was added successfully

  • false if the element already exists in the HashSet

Using Add() Method with String HashSet

Here's how to add string elements to a HashSet −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(string[] args) {
      HashSet<string> set1 = new HashSet<string>();
      set1.Add("A");
      set1.Add("B");
      set1.Add("C");
      set1.Add("D");
      set1.Add("E");
      set1.Add("F");
      set1.Add("G");
      set1.Add("H");
      
      Console.WriteLine("Elements in HashSet1...");
      foreach (string res in set1) {
         Console.WriteLine(res);
      }
      
      HashSet<string> set2 = new HashSet<string>();
      set2.Add("John");
      set2.Add("Jacob");
      set2.Add("Ryan");
      set2.Add("Tom");
      set2.Add("Andy");
      set2.Add("Tim");
      set2.Add("Steve");
      set2.Add("Mark");
      
      Console.WriteLine("Elements in HashSet2... (Enumerator iterating through HashSet)");
      HashSet<string>.Enumerator demoEnum = set2.GetEnumerator();
      while (demoEnum.MoveNext()) {
         string res = demoEnum.Current;
         Console.WriteLine(res);
      }
      
      Console.WriteLine("Is HashSet1 equal to HashSet2? = " + set1.Equals(set2));
   }
}

The output of the above code is −

Elements in HashSet1...
A
B
C
D
E
F
G
H
Elements in HashSet2... (Enumerator iterating through HashSet)
John
Jacob
Ryan
Tom
Andy
Tim
Steve
Mark
Is HashSet1 equal to HashSet2? = False

Using Add() Method with Integer HashSet

Here's an example demonstrating adding integer elements and checking subset relationships −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(string[] args) {
      HashSet<int> set1 = new HashSet<int>();
      set1.Add(100);
      set1.Add(200);
      
      Console.WriteLine("Elements in HashSet1...");
      foreach (int res in set1) {
         Console.WriteLine(res);
      }
      
      HashSet<int> set2 = new HashSet<int>();
      set2.Add(100);
      set2.Add(200);
      
      Console.WriteLine("Elements in HashSet2...");
      foreach (int res in set2) {
         Console.WriteLine(res);
      }
      
      Console.WriteLine("Is set1 a subset of set2? " + set1.IsSubsetOf(set2));
   }
}

The output of the above code is −

Elements in HashSet1...
100
200
Elements in HashSet2...
100
200
Is set1 a subset of set2? True

Handling Duplicate Elements

HashSet automatically prevents duplicate elements. Here's an example showing the behavior when adding duplicate values −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(string[] args) {
      HashSet<string> colors = new HashSet<string>();
      
      bool result1 = colors.Add("Red");
      bool result2 = colors.Add("Blue");
      bool result3 = colors.Add("Red"); // Duplicate
      
      Console.WriteLine("Adding 'Red' first time: " + result1);
      Console.WriteLine("Adding 'Blue': " + result2);
      Console.WriteLine("Adding 'Red' again: " + result3);
      
      Console.WriteLine("\nElements in HashSet:");
      foreach (string color in colors) {
         Console.WriteLine(color);
      }
      
      Console.WriteLine("Total elements: " + colors.Count);
   }
}

The output of the above code is −

Adding 'Red' first time: True
Adding 'Blue': True
Adding 'Red' again: False

Elements in HashSet:
Red
Blue
Total elements: 2

Conclusion

The Add() method is the primary way to insert elements into a HashSet in C#. It returns true for successful additions and false for duplicates, making HashSet ideal for maintaining collections of unique elements efficiently.

Updated on: 2026-03-17T07:04:36+05:30

369 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements