C# HashSet<T> is a collection class in the .NET framework that represents a set of unique elements. It is part of the System.Collections.Generic namespace. HashSet<T> provides fast lookup and retrieval operations for adding, removing, and testing for the presence of elements based on their values.
Here are some key points about HashSet<T>:
- Unique Elements: HashSet<T> ensures that all elements in the collection are unique. If you attempt to add duplicate elements, they will be ignored.
- Performance: HashSet<T> provides efficient performance for operations such as adding, removing, and checking for the presence of elements. These operations have an average time complexity of O(1) (constant time).
- Unordered Collection: HashSet<T> does not maintain any particular order of elements. If you need to preserve the order, you should consider using a different collection type, such as List<T> or SortedSet<T>.
- Generic Type: The “T” in HashSet<T> represents the type of elements that the set can contain. It can be any valid C# data type or a custom class.
- Hashing: HashSet<T> internally uses hashing techniques to efficiently organize and locate elements. This allows for fast lookup and retrieval operations.
- Equality Comparison: HashSet<T> uses the default equality comparer for the element type T to determine whether two elements are equal or not. If you need custom equality logic, you can implement the IEqualityComparer<T> interface and provide a custom comparer when creating the HashSet<T>.
- Common Operations: HashSet<T> provides various methods for adding elements (Add), removing elements (Remove), checking for the presence of an element (Contains), clearing the set (Clear), and more. It also supports set operations such as Union, Intersect, Except, and SymmetricExcept.
Here’s an example of how to use HashSet<T>:
HashSet<string> set = new HashSet<string>(); set.Add("apple"); set.Add("banana"); set.Add("orange"); Console.WriteLine(set.Contains("apple")); // true Console.WriteLine(set.Contains("grape")); // false set.Remove("banana"); Console.WriteLine(set.Count); // 2 set.Clear(); Console.WriteLine(set.Count); // 0
In the example above, a HashSet<string> is created, and elements are added using the Add
method. The Contains
method is used to check if an element exists in the set. The Remove
method removes an element, and the Count
property gives the number of elements in the set. Finally, the Clear
method removes all elements from the set.
HashSet<T> is a versatile collection class that is commonly used when you need to store a unique set of elements and perform efficient lookup operations based on their values.
C# HashSet<T> example:
Certainly! Here’s an example that demonstrates the usage of HashSet<T> in C#:
using System; using System.Collections.Generic; class Program { static void Main() { // Create a HashSet of integers HashSet<int> numbers = new HashSet<int>(); // Add elements to the HashSet numbers.Add(5); numbers.Add(10); numbers.Add(15); // Check if an element exists in the HashSet Console.WriteLine(numbers.Contains(10)); // Output: True Console.WriteLine(numbers.Contains(20)); // Output: False // Add a duplicate element (will be ignored) numbers.Add(10); // Remove an element from the HashSet numbers.Remove(5); // Print the elements in the HashSet foreach (int number in numbers) { Console.WriteLine(number); } } }
In this example, a HashSet<int> named numbers
is created. Elements (integers) are added to the set using the Add
method. The Contains
method is used to check if a specific element exists in the set. The Remove
method removes an element from the set. Finally, the foreach
loop is used to iterate over the elements in the HashSet and print them to the console.
Output:
10 15
Note that the order of the elements in the HashSet may vary, as HashSet does not guarantee a specific order. Additionally, duplicate elements are not allowed in the HashSet, so adding a duplicate element will have no effect on the set.