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 C# | Copying the Hashtable elements to an Array Instance Hashtable.CopyTo(Array, Int32) Method is used to copy the elements of a Hashtable to a one-dimensional Array instance at the specified index.Syntax: public virtual void CopyTo (Array array, int arrayIndex); Parameters: array : The one-dimensional Array that is the destination of the DictionaryEntry 3 min read HashSet add() Method in Java The HashSet add() method in Java is used to add a specific element to the set. This method will add the element only if the specified element is not present in the HashSet. If the element already exists, the method will not add it again. This method ensures that no duplicate elements are added to th 2 min read C# HashSet Class In C#, a HashSet<T> class is an unordered collection of 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. In terms of performance, it is generally better than a list. It doe 6 min read C# | Check if a HashSet contains the specified element 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. HashSet.C 2 min read C# HashSet HashSet in C# is an unordered collection of unique elements. This collection is introduced in .NET 3.5. It supports the implementation of sets and uses the hash table for storage. This collection is of the generic type collection and it is defined under System.Collections.Generic namespace. It is ge 4 min read Like