Share via


When to use HashSet class instead of list

Question

Monday, October 9, 2017 12:48 PM

tell me what is HashSet class and its advantage?

what is the diference between HashSet and list class?

when to use HashSet  and when to use list class ?

All replies (2)

Monday, October 9, 2017 4:02 PM ✅Answered | 2 votes

Use a List<T> by default for managing lists of items. Use a Collection<T> if you need to expose this list publicly. Use Dictionary<K, V> when you need to store values based upon some unique key. This is covered in the official documentation.

Honestly, you probably won't need HashSet very often. It's usage is defined in the documentation and therefore does not need to be repeated in the forums. The key usage for a hashset, as discussed in the documentation, is performance when duplicate items are not allowed. You can implement duplicate detection in a List<T> but it requires more work.

LINQ itself also mostly eliminates the need for hashsets because it can do all the same set operations that hashset was designed for. However the other key scenario is memory optimization. LINQ will return new objects of IEnumerable<T> to represent the set. Under the hood this is deferred executed. Given large sets of data this can be expensive. Hashset works on the collection in memory so any set operations are changing the underlying data directly. If this is what you want then hashset would be preferred over LINQ.

Michael Taylor
http://www.michaeltaylorp3.net

Michael Taylor http://www.michaeltaylorp3.net


Tuesday, October 10, 2017 5:21 AM ✅Answered | 1 vote

Hi Sudip_inn,

>>HashSet class and its advantage

Simple summary: HashSet is to store a series of unique values.

Advantages:

Represents a set of values and provides high-performance operations.
This is a set of collections that do not contain duplicate elements, and the stored elements do not have a specific order.
The hash class has no maximum capacity for the number of elements stored in it. With the increase in the number of elements, this capacity is increasing

HashSet<T> Class:
https://msdn.microsoft.com/en-us/library/bb359438(v=vs.110).aspx

>>what is the diference between HashSet and list class?

list class : Add value, do not check any duplicate values.

>> when to use HashSet  and when to use list class ?

HashSet <T> class is mainly designed to do high-performance set operations, such as the intersection of two sets intersection, the set of differences and so on. The collection contains a set of elements that do not repeat and have no attribute order, and the HashSet rejects the duplicate object.

The amount of data is small, do not need complex operations, you can use the list.

Best Regards,

Yohann Lu

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].