Comments

5 C# Collections that Every C# Developer Must Know

Finding the right collection in .NET is like finding the right camera in a camera shop! There are so many options to choose from, and each is strong in certain scenarios and weak in others. If looking for a collection in .NET has left you confused, you’re not alone.

In this post, which is the first in the series on .NET collections, I’m going to cover 5 essential collection types that every C# developer must know. These are the collections that you’ll use 80 – 90% of the time, if not more. In the future posts in this series, I’ll be covering other collection types that are used in special cases, where performance and concurrency are critical.

So, in this post, I’m going to explore the following collection types. For each type, I’ll explain what it is, when to use and how to use it.

  • List
  • Dictionary
  • HashSet
  • Stack
  • Queue

List<T>

Represents a list of objects that can be accessed by an index. <T> here means this is a generic list. If you’re not familiar with generics, check out my YouTube video.

Unlike arrays that are fixed in size, lists can grow in size dynamically. That’s why they’re also called dynamic arrays or vectors. Internally, a list uses an array for storage. If it becomes full, it’ll create a new larger array, and will copy items from the existing array into the new one.

These days, it’s common to use lists instead of arrays, even if you’re working with a fixed set of items.

To create a list:

var list = new List<int>();

If you plan to store large number of objects in a list, you can reduce the cost of reallocations of the internal array by setting an initial size:

// Creating a list with an initial size
var list = new List<int>(10000);

Here are some useful operations with lists:

// Add an item at the end of the list
list.Add(4);

// Add an item at index 0
list.Insert(4, 0);

// Remove an item from list
list.Remove(1);

// Remove the item at index 0
list.RemoveAt(0);

// Return the item at index 0
var first = list[0];

// Return the index of an item
var index = list.IndexOf(4);

// Check to see if the list contains an item
var contains = list.Contains(4);

// Return the number of items in the list 
var count = list.Count;

// Iterate over all objects in a list
foreach (var item in list)
    Console.WriteLine(item);

Now, let’s see where a list performs well and where it doesn’t.

Adding/Removing Items at the Beginning or Middle

If you add/remove an item at the beginning or middle of a list, it needs to shift one or more items in its internal array. In the worst case scenario, if you add/remove an item at the very beginning of a list, it needs to shift all existing items. The larger the list, the more costly this operation is going to be. We specify the cost of this operation using Big O notation: O(n), which simply means the cost increases linearly in direct proportion to the size of the input. So, as n grows, the execution time of the algorithm increases in direct proportion to n.

Adding/Removing Items at the End

Adding/removing an item at the end of a list is a relatively fast operation and does not depend on the size of the list. The existing items do not have to be shifted. This is why the cost of this operation is relatively constant and is not dependent on the number of items in the list. We represent the execution cost of this operation with Big O notation: O(1). So, 1 here means constant.

Searching for an Item

When using methods that involve searching for an item(e.g. IndexOf, Contains and Find), List performs a linear search. This means, it iterates over all items in its internal array and if it finds a match, it returns it. In the worst case scenario, if this item is at the end of the list, all items in the list need to be scanned before finding the match. Again, this is another example of O(n), where the cost of finding a match is linear and in direct proportion with the number of elements in the list.

Accessing an Item by an Index

This is what lists are good at. You can use an index to get an item in a list and no matter how big the list is, the cost of accessing an item by index remains relatively constant, hence O(1).

List in a Nutshell

So, adding/removing items at the end of a list and accessing items by index are fast and efficient operations with O(1). Searching for an item in a list involves a linear search and in the worst case scenario is O(n). If you need to search for items based on some criteria, and not an index (e.g. customer with ID 1234), you may better use a Dictionary.

 

Dictionary<TKey, TValue>

Dictionary is a collection type that is useful when you need fast lookups by keys. For example, imagine you have a list of customers and as part of a task, you need to quickly look up a customer by their ID (or some other unique identifier, which we call key). With a list, looking up a customer involves a linear search and the cost of this operation, as you learned earlier, is O(n) in the worst case scenario. With a dictionary, however, look ups are very fast with O(1), which means no matter how large the dictionary is, the look up time remans relatively constant.

When storing or retrieving an object in a dictionary, you need to supply a key. The key is a value that uniquely identifies an object and cannot be null. For example, to store a Customer in a Dictionary, you can use CustomerID as the key.

To create a dictionary, first you need to specify the type of keys and values:

var dictionary = new Dictionary<int, Customer>();

Here, our dictionary uses int keys and Customer values. So, you can store a Customer object in this dictionary as follows:

dictionary.Add(customer.Id, customer);

You can also add objects to a dictionary during initialization:

var dictionary = new Dictionary<int, Customer>
{
     { customer1.Id, customer1 },
     { customer2.Id, customer2 }
}

Later, you can look up customers by their IDs very quickly:

// Return the customer with ID 1234 
var customer = dictionary[1234];

You can remove an object by its key or remove all objects using the Clear method:

// Removing an object by its key
dictionary.Remove(1);

// Removing all objects
dictionary.Clear();

And here are some other useful methods available in the Dictionary class:

var count = dictionary.Count; 

var containsKey = dictionary.ContainsKey(1);

var containsValue = dictionary.ContainsValue(customer1);

// Iterate over keys 
foreach (var key in dictionary.Keys)
     Console.WriteLine(dictionary[key]);

// Iterate over values
foreach (var value in dictionary.Values)
     Console.WriteLine(value);

// Iterate over dictionary
foreach (var keyValuePair in dictionary)
{
     Console.WriteLine(keyValuePair.Key);
     Console.WriteLine(keyValuePair.Value);
}

So, why are dictionary look ups so fast? A dictionary internally stores objects in an array, but unlike a list, where objects are added at the end of the array (or at the provided index), the index is calculated using a hash function. So, when we store an object in a dictionary, it’ll call the GetHashCode method on the key of the object to calculate the hash. The hash is then adjusted to the size of the array to calculate the index into the array to store the object. Later, when we lookup an object by its key, GetHashCode method is used again to calculate the hash and the index. As you learned earlier, looking up an object by index in an array is a fast operation with O(1). So, unlike lists, looking up an object in a dictionary does not require  scanning every object and no matter how large the dictionary is, it’ll remain extremely fast.

So, in the following figure, when we store this object in a dictionary, the GetHashCode method on the key is called. Let’s assume it returns 1234. This hash value is then adjusted based on the size of the internal array. In this figure, length of the internal array is 6. So, the remainder of the division of 1234 by 6 is used to calculate the index (in this case 4). Later, when we need to look up this object, its key used again to calculate the index.

Hashtable in C#

Now, this was a simplified explanation of how hashing works. There is more involved in calculation of hashes, but you don’t really need to know the exact details at this stage (unless for personal interests). All you need to know as a C# developer is that dictionaries are hash-based collections and for that reason lookups are very fast.

 

HashSet<T>

A HashSet represents a set of unique items, just like a mathematical set (e.g. { 1, 2, 3 }). A set cannot contain duplicates and the order of items is not relevant. So, both { 1, 2, 3 } and { 3, 2, 1 } are equal.

Use a HashSet when you need super fast lookups against a unique list of items. For example, you might be processing a list of orders, and for each order, you need to quickly check the supplier code from a list of valid supplier codes.

A HashSet, similar to a Dictionary, is a hash-based collection, so look ups are very fast with O(1). But unlike a dictionary, it doesn’t store key/value pairs; it only stores values. So, every objects should be unique and this is determined by the value returned from the GetHashCode method. So, if you’re going to store custom types in a set, you need to override GetHashCode and Equals methods in your type.

To create a HashSet:

var hashSet = new HashSet<int>();

You can add/remove objects to a HashSet similar to a List:

// Initialize the set using object initialization syntax 
var hashSet = new HashSet<int>() { 1, 2, 3 };

// Add an object to the set
hashSet.Add(4);

// Remove an object 
hashSet.Remove(3);

// Remove all objects 
hashSet.Clear();

// Check to see if the set contains an object 
var contains = hashSet.Contains(1);

// Return the number of objects in the set 
var count = hashSet.Count;

HashSet provides many mathematical set operations:

// Modify the set to include only the objects present in the set and the other set
hashSet.IntersectWith(another);

// Remove all objects in "another" set from "hashSet" 
hashSet.ExceptWith(another);

// Modify the set to include all objects included in itself, in "another" set, or both
hashSet.UnionWith(another);

var isSupersetOf = hashSet.IsSupersetOf(another);
var isSubsetOf = hashSet.IsSubsetOf(another);
var equals = hashSet.SetEquals(another);

 

Stack<T>

Stack is a collection type with Last-In-First-Out (LIFO) behaviour. We often use stacks in scenarios where we need to provide the user with a way to go back. Think of your browser. As you navigate to different web sites, these addresses that you visit are pushed on a stack. Then, when you click the back button, the item on the stack (which represents the current address in the browser) is popped and now we can get the last address you visited from the item on the stack. The undo feature in applications is implemented using a stack as well.

Here is how you can use a Stack in C#:

var stack = new Stack<string>();
            
// Push items in a stack
stack.Push("http://www.google.com");

// Check to see if the stack contains a given item 
var contains = stack.Contains("http://www.google.com");

// Remove and return the item on the top of the stack
var top = stack.Pop();

// Return the item on the top of the stack without removing it 
var top = stack.Peek();

// Get the number of items in stack 
var count = stack.Count;

// Remove all items from stack 
stack.Clear();

Internally, a stack is implemented using an array. Since arrays in C# have a fixed size, as you push items into a stack, it may need to increase its capacity by re-allocating a larger array and copying existing items into the new array. If re-allocation doesn’t need to happen, push is O(1) operation; otherwise, if re-allocation is required, assuming the stack has n elements, all these elements need to be copied to the new array. This leads to runtime complexity of O(n).

Pop is an O(1) operation.

Contains is a linear search operation with O(n).

 

Queue<T>

Queue represents a collection with First-In-First-Out (FIFO) behaviour. We use queues in situations where we need to process items as they arrive.

Three main operations on queue include:

  • Enqueue: adding an element to the end of a queue
  • Dequeue: removing the element at the front of the queue
  • Peek: inspecting the element at the front without removing it.

Here is how you can use a queue:

var queue = new Queue<string>();

// Add an item to the queue
queue.Enqueue("transaction1");

// Check to see if the queue contains a given item 
var contains = queue.Contains("transaction1");

// Remove and return the item on the front of the queue
var front = queue.Dequeue();

// Return the item on the front without removing it 
var top = queue.Peek();
            
// Remove all items from queue 
queue.Clear();

// Get the number of items in the queue
var count = queue.Count;

 

Summary

Lists are fast when you need to access an element by index, but searching for an item in a list is slow since it requires a linear search.

Dictionaries provide fast lookups by key. Keys should be unique and cannot be null.

HashSets are useful when you need fast lookups to see if an element exists in a set or not.

Stacks provide LIFO (Last-In-First-Out) behaviour and are useful when you need to provide the user with a way to go back.

Queues provide FIFO (First-In-First-Out) behaviour and are useful to process items in the order arrived.

 

Love your feedback!

If you enjoyed this post, please share it and leave a comment. If you got any questions, feel free to post them here. I’ll answer every question.

Related Posts

Tags: , ,

23 responses to “5 C# Collections that Every C# Developer Must Know”

  1. Jonathan says:

    You completely overlooked Collection, the recommended base class for any custom collection class.

    https://msdn.microsoft.com/en-us/library/ms132397(v=vs.110).aspx

    • admin says:

      Hi Jonathan,

      Thanks for bringing this up. I’m not entirely sure why one would need to create a custom collection given that we have a rich set of collection types in .NET! I personally never needed to create a custom collection, but assuming it may still be useful at times, I don’t think it fits in the 90/10 rule! :)

  2. Tajeddin says:

    Dear Sir
    This is to appreciate you for provided information.
    It was very useful due all point were mentioned together and very applicable,also appreciated if you let me have some information about Ienumerable and Getenumetor
    Best Regards,
    Behdad Tajeddin

  3. Blues says:

    Hi Mosh,

    Thank you so much for this. :) I am one of your students in Udemy and I must say that i’m always looking forward to your courses. :) Your courses shed a lot of light in my programming venture and this blog sure helped me ones again (this time its free though 😉 ).

    Thanks again,
    Udemy Student

    • admin says:

      Thanks so much for your beautiful comment. I’m glad you’ve enjoyed my courses. That’s the most rewarding part of this journey! :)

  4. thiswascool says:

    I learned something new about my favorite language thanks! a note: maybe add comments of results of operations to see what exactly every function is doing.

  5. Jay says:

    Good read for beginners who want to know what classes to focus their time on!

    Regarding the “Adding to the end of a List”, don’t you think it’s worth mentioning that if the list is full, it’s going to have to be copied to a new array?

    • Jay says:

      I definitely didn’t notice the “Notify me…” checkboxes below the Submit button on your mobile interface. Which is a shame because now I won’t get a notification from follow-up comments :(

      Maybe the tickboxes should be *above* the submit button?

      • admin says:

        Totally agree! Usability issue! Not really a fan of WordPress and PHP and I’ve spent some time cleaning things up in the template I’m using. There are still many things to improve. Thanks for letting me know Jay! :)

    • admin says:

      I believe I did cover that at the beginning of the List section!

  6. Nicky Liu says:

    Very useful, thanks.

  7. Rob S. says:

    A couple mistakes in your HashSet section…

    1) { 1, 2, 3 } and { 3, 2, 1 } are equivalent, but NOT equal. Be careful with your terminology here, since if you have two such hash sets and you use == or .Equals, you will get False.

    2) Your bit about GetHashCode isn’t really correct. First, uniqueness is NOT determinied by the value returned from the GetHashCode method. You can certainly have many values in a HashSet or Dictionary which are completely different, but have the same result from GetHashCode. The only requirement is that if 2 items are equivalent, Equals must return true, and GetHashCode must return the same value.

    Also, lookups are only fast if you have a good implementation of GetHashCode. It’s perfectly valid for GetHashCode to return 0 every time, but then you’ll degrade to O(n).

    • admin says:

      Hi Rob,

      Thanks for your input! I’m aware that GetHashCode does not guarantee uniqueness and it is possible that multiple objects with the same hash code get stored in the same bucket. However, I decided to exclude that level of detail from post so it would be valuable to all readers with different backgrounds. Again, thanks for your input!

  8. Roy says:

    Hi Mosh,

    I recently discovered your courses on Udemy and this site. Your courses are really easy to understand and has helped me better understand some of the concepts (Events and EventHandlers in particular). So thanks a lot for taking the time to do this.

    One thing, I did notice that you did not discuss adding to a Dictionary. What happens when there are repeated additions to the dictionary. Are the internal indexes re-arranged as the underlying array grows?

    • admin says:

      Hi Roy,

      Thanks for your kind words. I’m glad you’ve enjoyed my courses and videos on YouTube. Re your question, what exactly do you mean “when there are repeated additions to the dictionary”. Please clarify and I’ll do my best to answer your question.

  9. Daniel says:

    Hi Mosh,

    I’ve been programming with C# for a while now and was always troubled with this. Thanks so much for putting together this clear and concise guide. Would it be possible to add a section on Tuples?

    • admin says:

      Hi Daniel,

      I’m glad that you’ve enjoyed my article. Re Tuples, as I’ve explained in my course Clean Code, they are the lazy programmer’s approach to building software! Imagine you need a class to represent a concept like Position. Typically, you’d create a class called Position with two properties: X and Y. A lazy programmer who doesn’t want to create a new class, uses the Tuple class. This class is generic and based on the number of generic parameters you supply, it’ll have one or more properties. These properties will have names like Item1 and Item2, but it’s not clear what is Item1. Is it X or Y? Again, as I explained in my course Clean Code, avoid using it!

      • Daniel says:

        Thanks for pointing to your Clean Code course and indicating that Tuples make for a poor design choice. I’ll be sure to check it out.

  10. Daniel says:

    Mosh,

    Just another suggestion…could you add in information on how Dictionaries can store objects as values?

  11. sudhesh says:

    Hi mosh this post is especially very useful and clear to understand of how and when to use. But i can’t find the link to read all your articles from this site.

Leave a Reply