C# HashSet<T> Class

C# HashSet<T> Class

The HashSet<T> class in C# is a collection that contains unique elements and has no particular order. It's a part of the System.Collections.Generic namespace. A HashSet<T> is particularly useful when you need to avoid duplicates efficiently and check for the existence of items quickly.

Let's go through a tutorial on the HashSet<T> class:

1. Initialization:

You can initialize a HashSet<T> in various ways:

// Empty HashSet HashSet<int> numbers = new HashSet<int>(); // HashSet from an array int[] array = { 1, 2, 3, 4, 5 }; HashSet<int> setFromArray = new HashSet<int>(array); 

2. Adding Elements:

To add an element to the HashSet<T>, use the Add method:

numbers.Add(1); numbers.Add(2); numbers.Add(2); // Won't add since 2 is already present 

3. Removing Elements:

To remove an element, use the Remove method:

numbers.Remove(1); 

4. Checking for Existence:

To check if an item exists in the HashSet<T>, use the Contains method:

bool containsTwo = numbers.Contains(2); // true 

5. Iterating Over a HashSet:

You can iterate over a HashSet<T> using a foreach loop:

foreach (int num in numbers) { Console.WriteLine(num); } 

6. Set Operations:

HashSet<T> supports various set operations like union, intersection, and difference:

HashSet<int> setA = new HashSet<int> { 1, 2, 3, 4 }; HashSet<int> setB = new HashSet<int> { 3, 4, 5, 6 }; // Union setA.UnionWith(setB); Console.WriteLine(string.Join(", ", setA)); // 1, 2, 3, 4, 5, 6 // Intersection setA.IntersectWith(setB); Console.WriteLine(string.Join(", ", setA)); // 3, 4, 5, 6 // Difference setA.ExceptWith(setB); Console.WriteLine(string.Join(", ", setA)); // 1, 2 

7. Other Useful Methods:

  • Count: Returns the number of elements in the HashSet<T>.

    int count = numbers.Count; 
  • Clear: Removes all elements from the HashSet<T>.

    numbers.Clear(); 
  • IsSubsetOf and IsSupersetOf: Determines whether a HashSet<T> is a subset or superset of a specified collection.

    bool isSubset = setA.IsSubsetOf(setB); bool isSuperset = setA.IsSupersetOf(setB); 

Example:

Let's see an example where we find the unique characters in a string using a HashSet<T>:

using System; using System.Collections.Generic; namespace HashSetExample { class Program { static void Main() { string sentence = "Hello, world!"; HashSet<char> uniqueChars = new HashSet<char>(sentence); // Removing punctuation and whitespace for clarity uniqueChars.Remove(','); uniqueChars.Remove(' '); uniqueChars.Remove('!'); Console.WriteLine("Unique characters: " + string.Join(", ", uniqueChars)); } } } 

The output will be:

Unique characters: H, e, l, o, w, r, d 

Note that the order of the characters in the output may vary because HashSet<T> does not guarantee any specific order.

In summary, HashSet<T> is a powerful collection type in C# that offers efficient operations for sets. It's great when you need fast lookups and no duplicates.


More Tags

release timeoutexception bind row-height collectors ssas treeview sqldatareader spring-data-rest javapns

More Programming Guides

Other Guides

More Programming Examples