C# HashSet vs List – C# Programming Example

C# HashSet vs List

The Collection classes are a group of classes designed specifically for grouping together objects and performing tasks on them.

In the .NET framework, there are several classes available for performing these operations. Some of the classes are as follows:

  • List
  • Dictionary
  • HashSet
  • Queue

List: List class is a collection and defined in the System.Collections.Generic namespace and it provides the methods and properties like other Collection classes such as add, insert, remove, search etc. It’s the replacement for arrays, linked lists, queues, and most other one-dimensional data structures.

HashSet: A HashSet holds a set of objects, but in a way that it allows you to easily and quickly determine whether an object is already in the set or not. It does so by internally managing an array and storing the object using an index which is calculated from the hashcode of the object.


What is the difference between HashSet<T> and List<T>

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.

C# HashSet vs List Example









Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: