C# Tutorial

C# Tutorial C# First Application C# Variables C# Data Types C# Operators C# Keywords

C# Control Statement

C# If Statements C# Switch Statements C# for Loop C# While Loop C# do While loop C# Jump Statements C# Function C# functions with out variable

C# Arrays

C# Arrays

C# Function

C# Function call by value C# Call by reference C# Passing array to function C# Multidimensional Arrays C# Jagged Arrays C# Params C# Array Class C# Command Line Arguments

C# Object Class

C# Object and Classes C# Constructors C# Destructor C# this Keyword C# static field C# static class C# Static Constructor C# Structs C# enum C# Properties

C# Inheritance

C# Inheritance C# Multilevel Inheritance C# Aggregation C# Member overloading C# Method Overriding C# Base

C# Polymorphism

C# Polymorphism C# Sealed

C# Abstraction

C# Abstraction C# Interface

C# Namespace

C# Namespace C# Access Modifiers C# Encapsulation

C# Strings

C# String

C# Misc

C# Design Patterns Dictionary in C# Boxing and Unboxing in C# Ref and Out in C# Serialization in C# Dispose and Finalize in C# CONSOLE LOG IN C# Get File extension in C# Insert query in c# Difference Between List and Dictionary in C# Getters and Setters in C# Extension Methods in C# Insert query in c# CONSOLE LOG IN C# Get File extension in C# Random.NextDouble() Method in C# Binary Search in C# Difference between Delegates and Interfaces in C# Double.IsFinite() Method in C# Index Constructor in C# Abstraction in C# Basic OOPS Concepts In C# Queue.CopyTo() Method in C# single.compareto() method in C# C# Throw Exception in Constructor DECODE IN C# file.setlastwritetimeutc() method in C# Convert Object to List in C# convert.ToSByte(string, IFormatProvider) Method in C# C# Declare Delegate in Interface console.TreatControl C As Input property in C# Copying the queue elements to 1-D Array in C# Array.Constrainedcopy() Method in C# C# in and out Char.IsLetterOrDigit() method in C# Debugging in C# decimal.compare() method in C# Difference between Console.Read and Console.Readline in C# Metadata in C# C# Event Handler Example Default Interface Methods in C# Difference between hashtable and dictionary in C# C# program to implement IDisposable Interface Encapsulation in C# SortedList.IndexOfVaalue(Object) Method in C# Hash Maps in C# How to clear text files in C# How to Convert xls to xlsx in C# Foreach loop in C# FIFO in C# How to handle null exception in C# Type.Is Instance Of Type() Method in C# How to add data into MySQL database using C# How to use angular js in ASP net Csharp decimal.compare() method in Csharp Difference between Console.Read and Console.Readline in Csharp How to Implement Interface in Csharp char.IsUpper() Method in C# Index Of Any() Method in C# Quantifiers in C# C# program to Get Extension of a Given File C# Error Logging C# ENCRIPTION Can we create an object for Abstract Class in C# Console.CursorVisible in C# SortedDictionary Implementation in C# C# Hash Table with Examples Setting the Location of the Label in c# Collections in c# Virtual Keyword in C# Reverse of string in C# String and StringBuilder in C# Encapsulation in C# SortedList.IndexOfVaalue(Object) Method in C# Hash Maps in C# How to clear text files in C# How to Convert xls to xlsx in C# Foreach loop in C# FIFO in C# How to handle null exception in C# Type.Is Instance Of Type() Method in C# How to add data into MySQL database using C# How to use angular js in ASP net Csharp decimal.compare() method in Csharp Difference between Console.Read and Console.Readline in Csharp How to Implement Interface in Csharp char.IsUpper() Method in C# Index Of Any() Method in C# Quantifiers in C# C# program to Get Extension of a Given File Difference between ref and out in C# Singleton Class in C# Const And Readonly In Csharp BinaryReader and BinaryWriter in C# C# Attributes C# Delegates DirectoryInfo Class in C# Export and Import Excel Data in C# File Class in C# FileInfo Class in C# How to Cancel Parallel Operations in C#? Maximum Degree of Parallelism in C# Parallel Foreach Loop in C# Parallel Invoke in C# StreamReader and StreamWriter in C# TextReader and TextWriter in C# AsQueryable() in C# Basic Database Operations Using C# C# Anonymous Methods C# Events C# Generics C# Indexers C# Multidimensional Indexers C# Multithreading C# New Features C# Overloading of Indexers Difference between delegates and events in C# Operator overloading in C# Filter table in C# C# Queue with Examples C# Sortedlist With Examples C# Stack with Examples C# Unsafe Code File Handling in C# HashSet in C# with Examples List Implementation in C# SortedSet in C# with Examples C# in Depth Delegates and Events in C# Finally Block in C# How to Split String in C# Loggers in C# Nullable Types in C# REVERSE A STRING IN C# TYPE CASTING IN C# What is Generics in C# ABSTRACT CLASS IN C# Application of pointer in C# Await in c# READONLY AND CONSTANT IN C# Type safe in C# Types of Variables in c# Use of delegates in c# ABSTRACT CLASS IN C# Application of pointer in C# Await in c# READONLY AND CONSTANT IN C# Type safe in C# Types of Variables in c# Use of delegates in c# ABSTRACT CLASS IN C# Application of pointer in C# Await in c# READONLY AND CONSTANT IN C# Type safe in C# Types of Variables in c# Use of delegates in c# Atomic Methods Thread Safety and Race Conditions in C# Parallel LINQ in C# Design Principles in C# Difference Between Struct And Class In C# Difference between Abstraction and Encapsulation in C# Escape Sequence Characters in C# What is IOC in C# Multiple Catch blocks in C# Appdomain in C# Call back methods in C# Change Datetime format in C# Declare String array in C# Default Access Specifier in c# Foreach in LINQ C# How to compare two lists in C# How to Convert String to Datetime in c# How to get only Date from DateTime in C# Ispostback in asp net C# JSON OBJECT IN C# JSON STRINGIFY IN C# LAMBDA FUNCTION IN C# LINQ Lambda Expression in C# Microservices in C# MSIL IN C# Reference parameter in C# Shadowing(Method hiding) in C# Solid principles in C# Static Members in C# Task run in C# Transaction scope in C# Type Conversion in c# Unit of Work in C# Unit Test Cases in c# User Defined Exception in c# Using Keyword in C# Var Keyword in C# What is gac in C#

HashSet in C# with Examples

HashSet offers an effective method for handling collections of singular components. It belongs to the System.Collections.Generic namespace has several advantages over other collection types, particularly in situations when the uniqueness of the elements is essential.

Creating a HashSet

A HashSet can be easily created. You can begin by declaring a variable of type HashSetT>, initialising it using the constructor, and executing the code. The type of elements that the HashSet will hold are specified by the type of argument T. For instance, you would use HashSetint> to build a HashSet of integers.

Here is an example of initialising a HashSet of strings with some elements:

HashSet<string> mySet = new HashSet<string>()

{

    "apple",

    "banana",

    "mango"

};

In the code above, we declare a string storing HashSet called mySet. The initial members of the set are provided inside the curly braces after using the new keyword to create a new instance of the hash set.

Using HashSet, you may guarantee that each element in the collection is distinct. Duplicate elements will be immediately disregarded if you try to add them, preserving the set's unique feature.

Because a hash table is the fundamental data structure of HashSet, quick element insertion and retrieval are possible. This data structure facilitates quick access to elements by using a hash function to associate elements with buckets.

HashSet offers overloaded constructors and methods for customising the set's behaviour and starting capacity in addition to the default constructor. You can also define a custom equality comparison if you want to compare components according to a certain standard.

Utilising HashSet makes keeping unique collections easier and is helpful for various tasks, including removing duplicate items from a list, verifying that an item exists, and carrying out set operations like union, intersection, and Difference. It is the default option when working with collections in C# due to its effective performance and inherent uniqueness guarantee.

Iterating over a HashSet

In C#, you have many alternatives when iterating through a hash set's components. Utilising a foreach loop and utilising an Enumerator are the two methods used most frequently.

1. Using the Foreach loop

A hash set's components can be iterated overusing a foreach loop. This straightforward approach offers an easy way to reach each set member.

HashSet<string> fruits = new HashSet<string> { "apple", "banana", "orange" };

foreach (string fruit in fruits)

{

    Console.WriteLine(fruit);

}

2. Using Enumerator

The GetEnumerator function of the HashSet class, which implements the IEnumerable interface, enables you to get an Enumerator. The items of the HashSet can then be iterated by using this Enumerator.

HashSet<int> numbers = new HashSet<int> { 1, 2, 3 };

var enumerator = numbers.GetEnumerator();

while (enumerator.MoveNext())

{

    int number = enumerator.Current;

    Console.WriteLine(number);

}

Adding and Removing elements

To add elements to a HashSet, you can use the Add method. Here is an example:

HashSet<int> numbers = new HashSet<int>();

numbers.Add(10);

numbers.Add(20);

numbers.Add(30);

You can remove elements using the remove method.

using System;

using System.Collections.Generic;

class Program

{

    static void Main()

    {

        HashSet<int> numbers = new HashSet<int>();

        numbers.Add(10);

        numbers.Add(20);

        numbers.Add(30);

        numbers.Remove(20);

        foreach (int number in numbers)

        {

            Console.WriteLine(number);

        }

    }

}

Output:

HashSet in C# with Examples

Modifying Elements

When elements are added to a HashSet, they are regarded as immutable. This means that a HashSet's elements cannot be changed directly. You must remove the element before adding it again in the updated form to achieve the desired result.

Example:

using System;

using System.Collections.Generic;

class Program

{

    static void Main()

    {

        HashSet<string> names = new HashSet<string>();

        // Add some names to the set

        names.Add("Vardhan");

        names.Add("Vikas");

        names.Add("Vathnam");

        // Display the initial set

        Console.WriteLine("Initial Set:");

        foreach (string name in names)

        {

            Console.WriteLine(name);

        }

        // Modify an element by removing and re-adding it

        if (names.Contains("Vathnam"))

        {

            names.Remove("Vathnam");

            names.Add("Rathnam");

        }

        // Display the modified set

        Console.WriteLine("\nModified Set:");

        foreach (string name in names)

        {

            Console.WriteLine(name);

        }

    }

}

Output:

HashSet in C# with Examples

In this example, we build a HashSet with a string value of names and add some names. To change an element, we first determine whether the desired element is present in the set (Vathnam). If it does, we use the Remove() method to remove the element and the Add() function to add the updated version (Rathnam).

Note: Changing an element in a HashSetT> requires eliminating and re-adding elements, which may affect the set's effectiveness and performance. Consider adopting a different data structure that allows for direct modification, such as a List<T> or a Dictionary<TKey, TValue>, if you regularly need to modify records.

Checking if the elements Exist or not.

HashSet provides several methods to check for the presence of an element.

1. Using Contains method

One way to check if an element exists in a HashSet is by using the Contains method. It will return true if the element is found and false if it is not.

Example:

using System;

using System.Collections.Generic;

class Program

{

    static void Main()

    {

        HashSet<int> numbers = new HashSet<int>();

        numbers.Add(10);

        numbers.Add(20);

        numbers.Add(30);

        bool contains = numbers.Contains(30);

        Console.WriteLine(contains);

    }

}

Output:

HashSet in C# with Examples

2. Using Overlaps method: The Overlaps method checks if any elements in one HashSet exist in another HashSet. It returns true if there is at least one common element; otherwise, it returns false.

Example:

using System;

using System.Collections.Generic;

class Program

{

    static void Main()

    {

        HashSet<int> numbers = new HashSet<int>();

        numbers.Add(10);

        numbers.Add(20);

        numbers.Add(30);

        HashSet<int> otherNumbers = new HashSet<int> { 30, 40, 50 };

        bool hasCommonElements = numbers.Overlaps(otherNumbers);

        Console.WriteLine(hasCommonElements);

    }

}

Output:

HashSet in C# with Examples

3. Using the IntersectsWith method

Determine whether two HashSets share any components by using the IntersectsWith function. If there are shared elements, it returns true; otherwise, it returns false.

Example:

using System;

using System.Collections.Generic;

class Program

{

    static void Main()

    {

        HashSet<int> numbers = new HashSet<int>();

        numbers.Add(10);

        numbers.Add(20);

        numbers.Add(30);

        HashSet<int> otherNumbers = new HashSet<int> { 30, 40, 50 };

        bool hasCommonElements = numbers.Overlaps(otherNumbers);

        Console.WriteLine(hasCommonElements);

    }

}

Output:

HashSet in C# with Examples

Sorting and Ordering in HashSet

A HashSet<T> does not automatically retain a particular order or offer direct sorting capability. HashSet<T>'s elements are kept unordered for quick access and uniqueness

The HashSetT> can be transformed into another collection type, such as a List<T>, and then sorting operations can be used on that collection to sort or order the elements if necessary. Here's an example:

using System;

using System.Collections.Generic;

using System.Linq;

class Program

{

    static void Main()

    {

        HashSet<int> numbers = new HashSet<int>();

        // Add some numbers to the set

        numbers.Add(5);

        numbers.Add(2);

        numbers.Add(8);

        numbers.Add(3);

        numbers.Add(1);

        // Convert the set to a list and sort it

        List<int> sortedList = numbers.ToList();

        sorted list.Sort();

        // Display the sorted elements

        Console.WriteLine("Sorted Elements:");

        foreach (int number in sortedList)

        {

            Console.WriteLine(number);

        }

    }

}

Output:

HashSet in C# with Examples

Here, we have a HashSet<int> with the name numbers that contain some integer values. We use the ToList() function to change the HashSet<int> into a List<int> and then sort the elements. The elements in the list are then sorted in ascending order using the Sort() method. To display the elements in the proper order, we finally iterate over the sorted list.

The required sorting and ordering capability can be obtained by changing the HashSet<int> to another collection type and performing sorting operations on that collection. Remember that the sorted collection you produce contains the order rather than the HashSet<int> itself.

Searching in HashSet

You can use various techniques to find elements within the set. Here are three popular ways to search a HashSet:

The Contains() method determines whether a particular element is present in the HashSet. If the element is present or not, a boolean value is returned. The Contains() method internally employs an equality comparison and hash code to detect whether an element is present. It has an average temporal complexity of O(1).

The IntersectWith() method lets you identify the items two data collections share. This method will change the original set only to contain the elements shared by the two sets if you pass it another HashSet as an argument. This approach is helpful when you need to locate the intersection of two sets quickly.

The SetEquals() method determines whether two sets are equal, that is, whether they contain the same elements. It accepts a second HashSet as an input and provides a boolean response indicating whether or not the two sets are equal. The SetEquals() method uses an equality comparison and the hash code to compare the elements in the two sets. O(n), where n is the number of elements in the set, is the temporal complexity of the problem.

Example:

using System;

using System.Collections.Generic;

class Program

{

    static void Main()

    {

        // Create two sets

        HashSet<int> set1 = new HashSet<int>() { 1, 2, 3, 4, 5 };

        HashSet<int> set2 = new HashSet<int>() { 4, 5, 6, 7, 8 };

        // Check if an element exists in the set using Contains()

        int elementToCheck = 3;

        bool containsElement = set1.Contains(elementToCheck);

        Console.WriteLine($"Set1 contains {elementToCheck}: {containsElement}");

        // Find the common elements between two sets using IntersectWith()

        set1.IntersectWith(set2);

        Console.WriteLine("Common elements between Set1 and Set2:");

        foreach (int element in set1)

        {

            Console.WriteLine(element);

        }

        // Check if two sets are equal using SetEquals()

        bool areSetsEqual = set1.SetEquals(set2);

        Console.WriteLine($"Set1 and Set2 are equal: {areSetsEqual}");

    }

}

Output:

HashSet in C# with Examples

Set Operations

Union: A set that contains all of the distinctive items from both sets is the union of two sets, denoted by A and B. In other words, it merges all components from both sets into a single, unique set.

Intersection: A set that contains the elements shared by two sets, denoted by A and B, is called an intersection set. It only contains the components that are included in both sets.

Difference: The set that contains the elements from set A that are absent from set B is the Difference between the two sets, A and B. In other words, it doesn't include the components that both sets share.

Subset: If every element in set A is also present in set B, then set A is said to be a subset of set B. In other words, set B contains all the elements in set A.

Example:

using System;

using System.Collections.Generic;

class Program

{

    static void Main()

    {

        // Creating two HashSets

        HashSet<int> set1 = new HashSet<int> { 1, 2, 3, 4, 5 };

        HashSet<int> set2 = new HashSet<int> { 4, 5, 6, 7, 8 };

        // Union operation

        HashSet<int> unionSet = new HashSet<int>(set1);

        unionSet.UnionWith(set2);

        Console.WriteLine("Union:");

        foreach (int element in unionSet)

        {

            Console.WriteLine(element);

        }

        // Intersection operation

        HashSet<int> intersectionSet = new HashSet<int>(set1);

        intersectionSet.IntersectWith(set2);

        Console.WriteLine("\nIntersection:");

        foreach (int element in intersectionSet)

        {

            Console.WriteLine(element);

        }

        //Difference operation

        HashSet<int> differenceSet = new HashSet<int>(set1);

        differenceSet.ExceptWith(set2);

        Console.WriteLine("\nDifference:");

        foreach (int element in differenceSet)

        {

            Console.WriteLine(element);

        }

        // Subset operation

        bool isSubset = set1.IsSubsetOf(set2);

        Console.WriteLine("\nIs set1 a subset of set2? " + isSubset);

    }

}

Output:

HashSet in C# with Examples

The C# HashSet<T> class methods and operators can be used to carry out these set operations. For instance, you can conduct a union operation using the UnionWith() method, an intersection operation using the IntersectWith() method, and a difference operation using the ExceptWith() method.

Note: It is important to remember that when using HashSetT> to conduct set operations, the resulting set will automatically eliminate duplicate elements because HashSet<T> ensures each element is unique.

Performance and Complexity

HashSet's performance and complexity must be considered when working with this data structure. Let's talk about HashSet's performance features and complexity analysis:

Insertion, Deletion, and Searching:

Insertion (Add()): The temporal complexity of adding an element to a hash set is typically O(1). In the worst-case situation, the time complexity, where n is the set's element count, could be O(n). This happens when objects collide, growing the internal bucket structure.

Deletion (Remove()) It takes O(1) on average time to remove an element from a hash set. Similar to insertion, collisions can cause the worst-case time complexity to be O(n).

Searching (Contains()): A HashSet's average time complexity for determining whether a given element is present is O(1). It is important to remember that the Contains() method uses the element's hash code to find the appropriate bucket, producing a constant-time search quickly.

Memory Usage:

A HashSet uses memory in direct proportion to its number of entries. The internal bucket structure expands dynamically as elements are added to the set, effectively accommodating the elements. However, this may result in greater memory utilisation compared to a straightforward array or list.

Performance and Collisions:

When two or more elements generate the same hash code and are stored in the same bucket, this is referred to as a collision. In certain circumstances, HashSet's speed may be affected since it must search through the elements in the bucket to locate a particular element. However, HashSet uses a robust hashing algorithm and bucket structure to reduce collisions and, in most circumstances, provide reliable performance.

Benefits of HashSet

Efficient uniqueness checking: HashSet ensures that all elements are unique, which can be beneficial when dealing with large datasets.

Fast lookup: HashSet provides fast lookup operations due to its hash-based implementation.

Drawbacks of HashSet

Memory usage: HashSet requires additional memory to store hash codes and manage collisions, which can impact memory usage compared to more straightforward collection types like lists or arrays.

Ordering: HashSet does not maintain the order of elements as it focuses on uniqueness and performance.