dotnet / dotnet/dotnet-api-docs

Missing definition / explanation of HashSet

Open
#9,953 3 comments 0 reactions 0 assignees View on GitHub
area-System.Collections Pri3
Dominant language
C#
Stars
949
Forks
1.7k
Avg merge
3d 27m
Merged PRs (30d)
49

Description

The Learn page for HashSet (https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1?view=net-8.0#definition) has a severely lacking definition, and absolutely no explanation of what a HashSet is, what it might be used for, how it compares to other collections, etc.

From the Learn article, the "Definition" in its entirety is:
"Represents a set of values." - no kidding. How about adding a definition that might actually be useful to developers, like maybe:

A HashSet is a collection of unique elements. It’s like a box where you can put different items, but each item can only be in the box once. If you try to add an item that’s already in the box, the HashSet will simply ignore it.

Here’s why you might use a HashSet:

Uniqueness: If you need to ensure that all elements in your collection are unique, a HashSet is a great choice. It automatically removes duplicates.
Performance: HashSet is very fast when it comes to checking if it contains a certain item. This is because it uses a technique called hashing, which allows it to locate items quickly no matter how many items it holds.
Now, how is a HashSet different from other collections?

HashSet vs List: A List allows duplicates and keeps items in the order you added them. A HashSet does not allow duplicates and does not preserve order.
HashSet vs Dictionary: A Dictionary is like a HashSet, but each item (called a key) is associated with a value. It’s like a box where each item has a tag attached to it. A HashSet just has items, no tags.
HashSet vs SortedSet: A SortedSet is like a HashSet, but it always keeps its items in a certain order (usually from smallest to largest). A HashSet does not keep its items in any particular order.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.