C# | Add element to HashSet Last Updated : 01 Feb, 2019 Comments Improve Suggest changes Like Article Like Report A HashSet is an unordered collection of the unique elements. It comes under 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. Elements can be added to HashSet using HashSet.Add(T) Method. Syntax: mySet.Add(T item); Here mySet is the name of the HashSet. Parameter: item: The element to add to the set. Return Type: This method returns true if the element is added to the HashSet object. If the element is already present then it returns false. Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to add element to HashSet using System; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating a HashSet of odd numbers HashSet<int> odd = new HashSet<int>(); // Inserting elements in HashSet for (int i = 0; i < 5; i++) { odd.Add(2 * i + 1); } // Displaying the elements in the HashSet foreach(int i in odd) { Console.WriteLine(i); } } } Output: 1 3 5 7 9 Example 2: CSHARP // C# code to add element to HashSet using System; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating a HashSet of strings HashSet<string> mySet = new HashSet<string>(); // Inserting elements in HashSet mySet.Add("Geeks"); mySet.Add("GeeksforGeeks"); mySet.Add("GeeksClasses"); mySet.Add("GeeksQuiz"); // Displaying the elements in the HashSet foreach(string i in mySet) { Console.WriteLine(i); } } } Output: Geeks GeeksforGeeks GeeksClasses GeeksQuiz Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1.add?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Add element to HashSet S Sahil_Bansall Follow Improve Article Tags : Misc C# CSharp-method CSharp-Generic-HashSet CSharp-Generic-Namespace +1 More Practice Tags : Misc Similar Reads C# | Adding an element into the Hashtable The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.Add(Object, Object) Method is used to adds an element with the specified key and value into the Hashtable. Syntax: 2 min read C# | Remove all elements from a HashSet A HashSet is an unordered collection of the unique elements. It comes under the 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. HashS 2 min read C# | Number of elements in HashSet 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 u 3 min read C# | Remove the specified element from a HashSet A HashSet is an unordered collection of the unique elements. It comes under 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. HashSet 3 min read Java HashSet clone() Method The HashSet clone() method in Java is used to return a shallow copy of the given HashSet. It just creates a copy of the set.Syntax of HashSet clone() Methodpublic Object clone()Return Type: This method returns a new HashSet object that contains the same element as the original set.Example: This exam 1 min read Like