C# - Collections notes

author: Paul Kim

categories: csharp

tags: csharp

C# Collections notes

  • List
  • Dictionary
  • HashSet
  • Stack
  • Queue


Lists are also called dynamic arrays or vectors because it uses an array for storage that can grow in size dynamically. Once the array is full, all items are copied to a new larger array.

  • Adding/removing items at the end of list is O(1)O(1)
  • Adding/removing items at beginning/middle of list is O(n)O(n)
  • Accessing items by index is O(1)O(1)
  • Searching for items is O(n)O(n)
// create a list
var list = new List<int>();

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

// add item at end of list

// add item at index 0
list.insert(4, 0);

// remove item from list

// remove item at index 0

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

// return index of item
var index = list.IndexOf(4);

// check if list contains item
var contains = list.Contains(4);

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

// iterate over all items in list
foreach (var item in list)


Looking up a list involves a linear search and is O(n)O(n). However, a dictionary lookup is constant and is O(1)O(1).

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

// add item to dictionary
dictionary.Add(customer.Id, customer);

// create a dictionary and add items to dictionary
var dictionary = new Dictionary<int, Customer>
  { customer1.Id, customer1 },
  { customer2.Id, customer2 }

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

// Removing an object by its key

// Removing all objects

// get count
var count = dictionary.Count;

// check if dictionary contains key
var containsKey = dictionary.ContainsKey(1);

// check if dictionary contains value
var containsValue = dictionary.ContainsValue(customer1);

// Iterate over keys
foreach (var key in dictionary.Keys)

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

// Iterate over dictionary
foreach (var keyValuePair in dictionary)


var hashSet = new HashSet<int>();

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

// Add an object to the set

// Remove an object

// Remove all objects

// 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;

// Modify the set to include only the objects present in the set and the other set

// Remove all objects in "another" set from "hashSet"

// Modify the set to include all objects included in itself, in "another" set, or both

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


  • Pop - O(1)O(1) operation
  • Contains - O(n)O(n) operation
var stack = new Stack<string>();

// Push items in a stack

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

// 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


Three main operations on queue:

  • 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.
var queue = new Queue<string>();

// Add an item to the queue

// 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

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



© 2021 All rights reserved.