C# Collections
Working with Lists, Arrays, Dictionaries and other collections
Arrays
int[] numbers = new int[5]; # declare array
string[] names = {"Alice", "Bob"}; # initialize array
numbers[0] = 10; # set element
int first = numbers[0]; # get element
int length = numbers.Length; # get length
List
using System.Collections.Generic; # import namespace
List<int> list = new List<int>(); # create list
list.Add(10); # add element
list.Remove(10); # remove element
list[0] # access by index
list.Count # get count
list.Clear(); # remove all
Dictionary
Dictionary<string, int> dict = new Dictionary<string, int>(); # create dictionary
dict["age"] = 25; # add key-value pair
dict.Add("score", 100); # add pair
dict.Remove("age"); # remove by key
dict.ContainsKey("age") # check if key exists
HashSet
HashSet<int> set = new HashSet<int>(); # create set
set.Add(10); # add unique element
set.Remove(10); # remove element
set.Contains(10) # check if exists
Queue and Stack
Queue<int> queue = new Queue<int>(); # FIFO queue
queue.Enqueue(10); # add to queue
queue.Dequeue(); # remove from front
Stack<int> stack = new Stack<int>(); # LIFO stack
stack.Push(10); # add to stack
stack.Pop(); # remove from top