Lesson 67 min read

Arrays & Lists

Store a whole bunch of things in one place

Arrays — Fixed-Size Containers

An array is like a row of lockers. You decide how many lockers you want when you build it, and each locker gets a number starting from 0 (not 1 — computers are weird like that). You can put one thing in each locker and grab it back by its number.

The catch? Once you build the row, you can't add or remove lockers. The size is locked in — this is what computer scientists call a static array.

Array Basics

// Create an array with initial values
string[] planets = { "Mercury", "Venus", "Earth", "Mars" };
// Access by index (0-based!)
Console.WriteLine($"First planet: {planets[0]}");
Console.WriteLine($"Third planet: {planets[2]}");
Console.WriteLine($"Total planets: {planets.Length}");
// Modify an element
planets[3] = "Red Planet";
Console.WriteLine($"Mars is now: {planets[3]}");
// Create an empty array of size 5
int[] scores = new int[5]; // all zeros by default
scores[0] = 95;
scores[1] = 87;
// Loop through with foreach
Console.Write("Scores: ");
foreach (int s in scores)
Console.Write($"{s} ");
Console.WriteLine();
// Loop with index using for
for (int i = 0; i < planets.Length; i++)
Console.WriteLine($" [{i}] {planets[i]}");
Output
First planet:  Mercury
Third planet:  Earth
Total planets: 4
Mars is now:   Red Planet
Scores: 95 87 0 0 0 
  [0] Mercury
  [1] Venus
  [2] Earth
  [3] Red Planet

List<T> — The Flexible Array

Arrays are fast but rigid. List<T> is a dynamic array — like a magical expanding backpack — you can keep adding items and it grows automatically. You can also remove items, insert at specific positions, and search easily.

The <T> part means you tell the list what type it holds: List<int>, List<string>, etc. This is called a generic — it's type-safe, meaning you can't accidentally put a string in a list of ints.

In real-world C#, you'll use List<T> way more often than arrays.

List<T> in Action

// Create a list
List<string> snacks = new List<string> { "Chips", "Cookies", "Popcorn" };
// Add items
snacks.Add("Pretzels");
snacks.Add("Candy");
Console.WriteLine($"Snacks: {snacks.Count}"); // Count, not Length!
// Insert at a specific position
snacks.Insert(0, "Fruit"); // add at the beginning
// Remove items
snacks.Remove("Candy"); // remove by value
snacks.RemoveAt(2); // remove by index
// Check if item exists
Console.WriteLine($"Has Chips? {snacks.Contains("Chips")}");
// Print all snacks
foreach (string snack in snacks)
Console.WriteLine($" - {snack}");
// Sort alphabetically
snacks.Sort();
Console.WriteLine("\nSorted:");
foreach (string snack in snacks)
Console.WriteLine($" - {snack}");
Output
Snacks: 5
Has Chips? True
  - Fruit
  - Chips
  - Popcorn
  - Pretzels

Sorted:
  - Chips
  - Fruit
  - Popcorn
  - Pretzels

LINQ — Query Your Data Like a Boss

LINQ (Language Integrated Query) lets you filter, transform, and search collections with clean, readable code. Think of it as having a super-powered search engine built right into C#.

Two key methods to start with:

  • .Where() — filter: "give me only the items that match this rule"
  • .Select() — transform: "change each item into something else"

We'll dive deeper into LINQ in a later lesson, but here's a taste.

LINQ Basics with Collections

List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Where — filter items
var evens = numbers.Where(n => n % 2 == 0);
Console.Write("Evens: ");
foreach (int n in evens) Console.Write($"{n} ");
Console.WriteLine();
// Select — transform items
var doubled = numbers.Select(n => n * 2);
Console.Write("Doubled: ");
foreach (int n in doubled) Console.Write($"{n} ");
Console.WriteLine();
// Chain them together!
var bigEvensDoubled = numbers
.Where(n => n % 2 == 0)
.Where(n => n > 4)
.Select(n => n * 2);
Console.Write("Big evens doubled: ");
foreach (int n in bigEvensDoubled) Console.Write($"{n} ");
Console.WriteLine();
// Convert LINQ result to List
List<int> result = numbers.Where(n => n > 7).ToList();
Console.WriteLine($"Numbers > 7: [{string.Join(", ", result)}]");
Output
Evens: 2 4 6 8 10 
Doubled: 2 4 6 8 10 12 14 16 18 20 
Big evens doubled: 12 16 20 
Numbers > 7: [8, 9, 10]
Note: 📏 Array vs List rule of thumb: Use an array when the size is fixed and known (like days of the week). Use List for everything else — it's more flexible and you'll use it 90% of the time in real projects.

Quick check

What index is the FIRST element of an array in C#?

Continue reading

Dynamic ArraysData Structure
Resizing strategy, amortized O(1) push
Static ArraysData Structure
A row of numbered boxes — fast to grab, fixed in size
Lists & TuplesPython
Ordered collections you can grow, shrink, and slice
ArraysJava
Fixed-size boxes — declare, fill, and loop
LoopsMethods