Notes

C# - Collections notes

author: Paul Kim

categories: csharp

tags: csharp

C# Collections notes

  • List
  • Dictionary
  • HashSet
  • Stack
  • Queue

List

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
list.Add(4);

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

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

// remove item at index 0
list.RemoveAt(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)
  Console.WriteLine(item);

Dictionary

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
dictionary.Remove(1);

// Removing all objects
dictionary.Clear();

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

HashSet

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

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

  • Pop - O(1)O(1) operation
  • Contains - O(n)O(n) operation
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();

Queue

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

Source

ChromeEdgeFirefoxOpera

© 2021 paulkode.com. All rights reserved.