Graeme Hill's Dev Blog

Are you sure you want List? Maybe you really want HashSet

Star date: 2011.037

I often find that I get so used to using List<T> for my collections that I forget to consider whether it is really the right tool for the job. If the collection is not ordered, and you don't want duplicates then there are a lot of benefits to using HashSet<T>. For example with List<T> you'll find yourself writing code like this:

if (!myCollection.Contains(newString))

This is a little verbose and it's also inefficient. Every time you call Contains() the entire collection has to be scanned. With a HashSet<T> set operations are optimized and the code is simpler:


The other benefit is that you actually enforce the no duplicate rule since adding the same element more than once will have no effect. Here's what the MSDN documentation says about HashSet<T>:

The HashSet(Of T) class provides high-performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.

In practice I am finding that there are actually a lot of scenarios when I really want a set and not a list. So the next time you create a collection, think about whether you can use a HashSet.

comments powered by Disqus