Can you explain what is the difference between HashSet<T> and List<T> in .NET?
Maybe you can explain with an example in what cases HashSet<T> should be preferred against List<T> ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Unlike a List<> …
A HashSet is a List with no duplicate members.
Because a HashSet is constrained to contain only unique entries, the internal structure is optimised for searching (compared with a list) – it is considerably faster
Adding to a HashSet returns a boolean – false if addition fails due to already existing in Set
Can perform mathematical set operations against a Set: Union/Intersection/IsSubsetOf etc.
HashSet doesn’t implement IList only ICollection
You cannot use indices with a HashSet, only enumerators.
The main reason to use a HashSet would be if you are interested in performing Set operations.
Given 2 sets: hashSet1 and hashSet2
flies in comparison with an equivalent operation using LINQ. It’s also neater to write!