Aral Code

Collections & Methods Lesson 9 of 15

C# Arrays, Lists and Dictionaries

Store many values in C# arrays, the resizable List<T>, and key–value Dictionary<TKey, TValue> — add, remove, search, sort and loop through them.

C# gives you three main ways to store many values: arrays (fixed size), List<T> (resizable) and Dictionary<TKey, TValue> (look up by key). Most real programs use Lists and Dictionaries.

Arrays

int[] grades = { 88, 92, 79, 95, 84 };
Console.WriteLine($"First: {grades[0]}, last: {grades[^1]}");
Console.WriteLine($"Count: {grades.Length}");

grades[2] = 81;
Array.Sort(grades);
Console.WriteLine(string.Join(", ", grades));
Console.WriteLine($"Average: {grades.Average():F2}");

Output

First: 88, last: 84
Count: 5
81, 84, 88, 92, 95
Average: 88.00

grades[^1] means "first from the end" (the last item). .Average() comes from LINQ, which is available automatically in modern C# projects.

List<T>

var names = new List<string> { "Ana", "Ben" };
names.Add("Carla");
names.Insert(0, "Dan");
names.Remove("Ben");

Console.WriteLine($"Count: {names.Count}");
Console.WriteLine(string.Join(", ", names));
Console.WriteLine(names.Contains("Carla"));
Console.WriteLine(names.IndexOf("Carla"));

names.Sort();
Console.WriteLine(string.Join(", ", names));

Output

Count: 3
Dan, Ana, Carla
True
2
Ana, Carla, Dan
MemberDoes
Add(x) / Insert(i, x)Add at the end / at index i
Remove(x) / RemoveAt(i)Remove by value / by index
CountNumber of items (arrays use Length)
Contains(x), IndexOf(x)Search
Sort(), Reverse(), Clear()Reorder / empty

A list from user input

var scores = new List<double>();
while (true)
{
    Console.Write("Score (blank to finish): ");
    string input = Console.ReadLine() ?? "";
    if (input == "") break;
    if (double.TryParse(input, out double s)) scores.Add(s);
}
Console.WriteLine($"{scores.Count} scores, average {scores.Average():F2}, highest {scores.Max()}");

Output

Score (blank to finish): 88
Score (blank to finish): 92.5
Score (blank to finish): 79
Score (blank to finish): 
3 scores, average 86.50, highest 92.5

Dictionary<TKey, TValue>

var prices = new Dictionary<string, decimal>
{
    ["pandesal"] = 25m,
    ["kape"] = 8m,
    ["itlog"] = 9m
};

Console.WriteLine(prices["kape"]);
prices["kape"] = 10m;                     // update
prices["asukal"] = 70m;                   // add

if (prices.TryGetValue("gatas", out decimal p))
    Console.WriteLine($"Gatas: {p}");
else
    Console.WriteLine("No gatas");

foreach (var (item, price) in prices)
{
    Console.WriteLine($"{item,-10}{price,8:N2}");
}

Output

8
No gatas
pandesal     25.00
kape         10.00
itlog         9.00
asukal       70.00

Counting with a Dictionary

string[] votes = { "Ana", "Ben", "Ana", "Carla", "Ana", "Ben" };
var tally = new Dictionary<string, int>();
foreach (string v in votes)
{
    tally[v] = tally.GetValueOrDefault(v) + 1;
}
foreach (var (name, count) in tally)
{
    Console.WriteLine($"{name}: {count}");
}

Output

Ana: 3
Ben: 2
Carla: 1

2D arrays

int[,] seats = { { 1, 2, 3 }, { 4, 5, 6 } };
Console.WriteLine(seats[1, 2]);
Console.WriteLine($"{seats.GetLength(0)} rows, {seats.GetLength(1)} columns");

Output

6
2 rows, 3 columns

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. How do you get the number of items in a List?
2. What does arr[^1] mean?
3. What happens when you read a missing key with dict["x"]?
4. Which method adds an item to a List?

By Sir Reyel · Last updated 2026-12-04