You are reading the article Learn The Working And Examples Of C# Hashset updated in September 2023 on the website Happystarlongbien.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Learn The Working And Examples Of C# Hashset
Introduction to C# HashSetWeb development, programming languages, Software testing & others
The above syntax represents HashSet in C#. The type of hashset can also be represented by the capital letter T.
Working of Hashset in C#Hashset in C# is a unique collection of elements without any order. It comes under the namespace Systems.Collections.Generic and is used whenever we do not need any duplicates in our collection, i.e., it avoids insertion of duplicates into the collection and compares the performance of HashSet. HashSet provides better performance compared to list set operations provided by the HashSet gives high performance. In order to understand the working of hashset, let’s begin by creating a simple hashset program in C#.
Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HashSet { class Programcheck { static void Main(string[] args) { "Shobha", "Shivakumar", "Shardha" }; foreach(var nam in nameslist) { Console.WriteLine(nam); } Console.ReadKey(); } } }In the above program, a simple hashset of type strings is created. The strings Shobha, Shivakumar, and Shardha are added to the created hashset of type strings. The output of the above program is shown in the snapshot below:
As already explained, the hashset does not allow duplicate elements to be added in the collection. Let us try adding a duplicate element to the above-created hashset of type strings and understand the output of the program. Then, we can use add a method to add the elements into the collection.
Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HashSet { class Programcheck { static void Main(string[] args) { "Shobha", "Shivakumar", "Shardha" }; nameslist.Add("Shobha"); foreach(var nam in nameslist) { Console.WriteLine(nam); } Console.ReadKey(); } } }In the above program, a simple hashset of type strings is created. Then, the strings Shobha, Shivakumar, and Shardha are added to the created hashset of type strings. If we try to add a duplicate string Shobha to the created hashset using add method, no error is shown, but it does not add the duplicate string, and the same can be seen in the output. The output of the above program is shown in the snapshot below:
There is a method called Union with method present in hashset in C#. It is used to combine the elements present in two collections into a single collection on which it is called. Consider the below program.
Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HashSet { class Programcheck { static void Main(string[] args) { "Shobha", "Shivakumar", "Shardha" }; "Shobha", "Shivakumar", "Shardha", "Ravi", "Nagu" }; nameslist.UnionWith(nameslist1); foreach(var nam in nameslist) { Console.WriteLine(nam); } Console.ReadKey(); } } }In the above program, two simple hashsets of type strings are created. First, the strings Shobha, Shivakumar, and Shardha are added to the first hashset of type strings. Then, the strings Shobha, Shivakumar, Shardha, Ravi, and Nagu are added to the second hashset of type strings. Now we make use of union with the method is hashset to combine the elements of both first hashset and second hashset. The output of the above program is shown in the snapshot below:
There is a method called Intersect with the method present in hashset in C#. It is used to combine the elements present in common in the two collections into a single collection on which it is called. Consider the below program.
Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HashSet { class Programcheck { static void Main(string[] args) { "Shobha", "Shivakumar", "Shardha" }; "Shobha", "Shivakumar", "Shardha", "Ravi", "Nagu" }; nameslist.IntersectWith(nameslist1); foreach(var nam in nameslist) { Console.WriteLine(nam); } Console.ReadKey(); } } }In the above program, two simple hashsets of type strings are created. First, the strings Shobha, Shivakumar, and Shardha are added to the first hashset of type strings. Next, the strings Shobha, Shivakumar, Shardha, Ravi, and Nagu are added to the second hashset of type strings. Now we use intersecting with the method is hashset to combine the common elements in both the first hashset and second hashset. The output of the above program is shown in the snapshot below:
There is a method called Except with method present in hashset in C#. It is used to remove all the elements present in all the two collections except the collection on which it is called. Consider the below program.
Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HashSet { class Programcheck { static void Main(string[] args) { "Shobha", "Shivakumar", "Shardha" }; "Shobha", "Shivakumar", "Shardha", "Ravi", "Nagu" }; nameslist1.ExceptWith(nameslist); foreach(var nam in nameslist1) { Console.WriteLine(nam); } Console.ReadKey(); } } }In the above program, two simple hashsets of type strings are created. First, the strings Shobha, Shivakumar, and Shardha are added to the first hashset of type strings. Next, the strings Shobha, Shivakumar, Shardha, Ravi, and Nagu are added to the second hashset of type strings. Now we make use of Except with the method in hashset to remove all the elements present in all the collections except the collection on which it is called. The output of the above program is shown in the snapshot below:
Examples of C# HashSet Example #1C# program to illustrate the creation of a hashset and adding elements to the hashset.
Code:
using System; using System.Collections.Generic; class Hashset { public static void Main() { for (int i = 0; i < 5; i++) { even.Add(2 * i); } foreach(int j in even) { Console.WriteLine(j); } } }Output:
Example #2C# program to illustrate contains method in a hashset.
Code:
using System; using System.Collections.Generic; class hashset { public static void Main() { cl.Add("Shobha"); cl.Add("Shivakumar"); cl.Add("Shardha"); cl.Add("Ravi"); if (cl.Contains("Shobha")) Console.WriteLine("The element specified is present"); else Console.WriteLine("The element specified is not present"); } }Output:
Recommended ArticlesThis is a guide to C# HashSet. Here we discuss the concept of Hashset in C# through definition and then understand the syntax and working of Hashset in C# through example programs. You may also have a look at the following articles to learn more –
You're reading Learn The Working And Examples Of C# Hashset
Update the detailed information about Learn The Working And Examples Of C# Hashset on the Happystarlongbien.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!