If I am not mistaken, some other standard collections have (or can be achieved) similar behavior and, moreover, other collections may have more opportunities than HashSet.
What is the highlight of HashSet and what tasks is it intended for?
If I am not mistaken, some other standard collections have (or can be achieved) similar behavior and, moreover, other collections may have more opportunities than HashSet.
What is the highlight of HashSet and what tasks is it intended for?
HashSet
, unlike ordinary collections, contains unique values. This is a hash table (as Dictionary<K, V>
), in which keys are also values at the same time. Uniqueness is determined by using a hashcode of values and checking them for equality. Therefore, if you use HashSet
for your types, you need to correctly override Equals()
and GetHashCode()
.
Example :
var set = new HashSet<int>(); Console.WriteLine(set.Add(42) ? "Added" : "Not added"); Console.WriteLine(set.Count); // повторим Console.WriteLine(set.Add(42) ? "Added" : "Not added"); Console.WriteLine(set.Count);
Common HashSet
Usage Scenarios:
HashSet
immediately)Source: https://ru.stackoverflow.com/questions/554492/
All Articles