Techiehook Techiehook
Updated date Jun 18, 2024
This article shows how to use HashSet in C# with sample code and output - providing the creation, manipulation, and operations on a HashSet

What is HashSet in C#?

A HashSet is a collection that stores only unique elements using a hash table, this is used to enable faster retrieval of elements compared to other collection types.

C# Program to show the use of HashSet in C#:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // Create a new HashSet of strings
        HashSet<string> cars = new HashSet<string>();

        // Add elements to the HashSet
        cars.Add("Audi");
        cars.Add("Ford");
        cars.Add("Mazda");
        cars.Add("Tesla");
        cars.Add("Mazda");// Duplicate element, will not be added

        // Display the elements in the HashSet
        Console.WriteLine("Cars in the HashSet:");
        foreach (var car in cars)
        {
            Console.WriteLine(car);
        }

        // Check if an element exists in the HashSet
        string carToCheck = "Ford";
        if (cars.Contains(carToCheck))
        {
            Console.WriteLine($"\n{carToCheck} is in the HashSet.");
        }
        else
        {
            Console.WriteLine($"\n{carToCheck} is not in the HashSet.");
        }

        // Remove an element from the HashSet
        cars.Remove("Tesla");
        Console.WriteLine("\nAfter removing 'Tesla':");
        foreach (var car in cars)
        {
            Console.WriteLine(car);
        }

        // Get the number of elements in the HashSet
        Console.WriteLine($"\nNumber of elements in the HashSet: {cars.Count}");

        // Clear all elements from the HashSet
        cars.Clear();
        Console.WriteLine($"\nNumber of elements in the HashSet after clearing: {cars.Count}");
        Console.ReadKey();
    }
}

We have the below operations in the above program,

  • Creating a HashSet: The HashSet<string> named cars is created to store a collection of unique strings.
  • Adding Elements: Elements are added to the HashSet using the Add method. If an element already exists, it will not be added again.
  • Displaying Elements: The elements in the HashSet are displayed using a foreach loop.
  • Checking for Existence: The Contains method checks if a specific element exists in the HashSet.
  • Removing Elements: The Remove method removes an element from the HashSet.
  • Getting the Count: The Count property returns the number of elements in the HashSet.
  • Clearing the HashSet: The Clear method removes all elements from the HashSet.

Output:

Cars in the HashSet:
Audi
Ford
Mazda
Tesla

Ford is in the HashSet.

After removing 'Tesla':
Audi
Ford
Mazda

Number of elements in the HashSet: 3

Number of elements in the HashSet after clearing: 0

ABOUT THE AUTHOR

Techiehook
Techiehook
Admin, Australia

Welcome to TechieHook.com! We are all about tech, lifestyle, and more. As the site admin, I share articles on programming, tech trends, daily life, and reviews... For more detailed information, please check out the user profile

https://www.techiehook.com/profile/alagu-mano-sabari-m

Comments (1)

  • Meeloun Education
    Meeloun Education 11 Jul, 2024

    审校与修改是确保coursework代写 http://australiaway.org/a/courseworkdaixie/ 获得满意分数的必要步骤。在完成初稿后,代写者应进行全面细致的审校和修改,确保作业的质量和规范性。审校内容应包括语言表达、格式规范、数据准确性、逻辑连贯性、引用和参考文献等方面。代写者应仔细检查作业的语法、拼写、标点等语言问题,确保语言准确、流畅;检查作业的格式是否符合要求,如标题、段落、行距、页码等;检查数据是否准确无误,分析是否合理严谨;检查作业的逻辑是否清晰,内容是否连贯一致;检查引用和参考文献是否正确、完整。此外,代写者还应根据客户的反馈和要求进行相应的修改和完善,确保作业符合客户的预期和标准。通过全面细致的审校和修改,代写者能够提高作业的整体质量和规范性,从而获得高分。