Understanding HashSet in C#

Bizznessia .NET

Bizznessia .NET

Programming Language

Published 2 months ago Viewed 0 times

HashSet<T> is a collection type in C# that provides a set of functionalities specifically designed for storing unique elements. It is part of the System.Collections.Generic namespace and is often used when you need to maintain a collection of distinct items while providing efficient search, insertion, and deletion operations.

When to Use HashSet

Using HashSet<T> is particularly beneficial in several scenarios:

  1. Uniqueness: If you want to ensure that no duplicates are present in your collection, HashSet automatically handles uniqueness. When an attempt is made to add a duplicate element, it will be ignored.

  2. Performance: HashSet<T> offers average time complexity of O(1) for operations like add, remove, and contains. This makes it a suitable choice for applications that require fast lookups and modifications.

  3. Set Operations: It supports various set operations such as union, intersection, and difference, which can be particularly useful for mathematical operations and data manipulation tasks.

How HashSet Works

HashSet<T> utilizes a hash table to store its elements. When an object is added to a HashSet, it computes the hash code of the object and uses this to determine where to store it in the table. This implementation ensures that both retrieval and storage operations are optimized for speed.

Key Characteristics

  • Non-ordered: The elements in a HashSet do not have a specific order. This means that the order in which items are added is not preserved.

  • Generics: HashSet<T> is a generic collection, which allows it to store any type of object, provided that the type supports equality checks.

  • Object Comparisons: To correctly determine uniqueness, the HashSet relies on the GetHashCode and Equals methods of the objects it contains. This means that if you’re storing custom objects, you need to override these methods appropriately to define what makes your objects unique.

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        HashSet<Employee> employees = new HashSet<Employee>();
        
        employees.Add(new Employee 
        { 
            Id = 1, 
            Name = "Alice", 
            Age = 30, 
            Position = "Developer", 
            Salary = 70000, 
            Department = "IT", 
            Email = "alice@example.com", 
            Phone = "123-456-7890", 
            HireDate = new DateTime(2020, 5, 1), 
            IsActive = true 
        });

        employees.Add(new Employee 
        { 
            Id = 2, 
            Name = "Bob", 
            Age = 25, 
            Position = "Designer", 
            Salary = 60000, 
            Department = "Design", 
            Email = "bob@example.com", 
            Phone = "234-567-8901", 
            HireDate = new DateTime(2021, 6, 15), 
            IsActive = true 
        });

        // Duplicate employee (same Id) won't be added
        employees.Add(new Employee 
        { 
            Id = 1, 
            Name = "Alice", 
            Age = 30, 
            Position = "Developer", 
            Salary = 70000, 
            Department = "IT", 
            Email = "alice@example.com", 
            Phone = "123-456-7890", 
            HireDate = new DateTime(2020, 5, 1), 
            IsActive = true 
        });

        // Displaying employees in the HashSet
        foreach (var employee in employees)
        {
            Console.WriteLine("{0}, {1}, {2} years", employee.Name, employee.Position, employee.Age);
        }
    }
}

public class Employee : IEquatable<Employee>
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public string Position { get; set; }
    public decimal Salary { get; set; }
    public string Department { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public DateTime HireDate { get; set; }
    public bool IsActive { get; set; }

    // Implementing IEquatable
    public bool Equals(Employee other)
    {
        if (other == null) return false;
        return Id == other.Id; // Compare by Id for uniqueness
    }

    // Overriding GetHashCode
    public override int GetHashCode()
    {
        return Id.GetHashCode(); // Use Id for hash code
    }

    // Also override Equals for object comparison
    public override bool Equals(object obj)
    {
        return Equals(obj as Employee); // Call the IEquatable implementation
    }
}

In summary, HashSet<T> is an excellent choice for scenarios requiring a collection of unique elements with fast access and modification capabilities. Its built-in mechanisms for enforcing uniqueness and supporting set operations make it a valuable tool in a C# developer's arsenal.

Effortless content and community for innovators and business minds

Join to unlock full access and engage

Members enjoy exclusive features! Create an account or sign in for free to comment, engage with the community, and earn reputation by helping others.

Create account

More posts from Bizznessia .NET

bizznessia.dev is a community where developers can learn business, collaborate with like-minded individuals, and turn their ideas into reality. We’re here to support you every step of the way in achieving your goals