───✱*.。:。✱*.:。✧*.。✰*.:。✧*.。:。*.。✱ ───
Arrays
- Homogenous, fixed-length data structure
- Size is determined on initialization
- Since
Array
is a fixed-length data structure, you need to copy them to add data.- Although you’re better off using a
List
at this point
- Although you’re better off using a
static T[] AddToArray<T>(T[] array, T item)
{
var newArr = new T[array.Length + 1];
for (var i = 0; i < array.Length; i++) newArr[i] = array[i];
newArr[^1] = item;
return newArr;
}
Multi-Dimensional Arrays
- Arrays that have more than one dimension, such as a grid
- All rows must have the same length
var map = new int[3, 3, 3]; // 3x3x3 — 3d array
Jagged Arrays
- Arrays of arrays where the rows can be different lengths
- More memory efficient in certain scenarios over multi-dimensional arrays
var jaggedArr = new int[3][][]; // 3 rows
Stack
- LIFO → last in, first out
- The most recently added item is the one that gets removed when you remove an item
Stack<T>.Push
→ adds item to the top of the stackStack<T>.Pop
→ removes items from the top of the stack
var cardStack = new Stack<Card>();
cardStack.Push(new Card("Queen", "Hearts"));
cardStack.Push(new Card("Ace", "Diamonds"));
cardStack.Push(new Card("Six", "Spades"));
cardStack.Pop(); // Six of Spades
Queue
- FIFO → first in, first out
- Items are removed sequentially, in the order that they arrived
Queue<T>.Enqueue
→ adds item to the front of the queueQueue<T>.Dequeue
→ removes items from the back of the queue
var cardQueue = new Queue<Card>();
cardQueue.Enqueue(new Card("King", "Clubs"));
cardQueue.Enqueue(new Card("Ten", "Hearts"));
cardQueue.Enqueue(new Card("Two", "Diamonds"));
cardQueue.Dequeue(); // King of Clubs
Dictionary
- Unordered, key/value pairs
Dictionary<T>.Remove
→ removes an item from the dictionaryDictionary<T>[Item]
→ access the item with keyItem
in the dictionary
var areas = new Dictionary<string, string>();
areas.Add("Square", "s^2");
areas.Add("Circle", "πr^2");
areas.Add("Triangle", "1/2bh");
Console.WriteLine(areas["Circle"]); // πr^2
───✱*.。:。✱*.:。✧*.。✰*.:。✧*.。:。*.。✱ ───