๐Ÿ  VisualStudioTutor.com  ยท  C# Tutorial Home  ยท  C# Lesson 8 of 40
Lesson 8 of 40 Core C# Intermediate โฑ 30 min

Collections โ€” Arrays, Lists & Dictionaries

Store and manage data with C#'s built-in collection types โ€” arrays, List<T>, Dictionary<K,V>, HashSet<T>, Queue, Stack, and immutable collections.

Part 1: Arrays

int[] scores = [95, 82, 77, 91]; // Collection expression (C# 12+)
Console.WriteLine(scores[0]); // 95
Console.WriteLine(scores[^1]); // 91 (last element)
int[] slice = scores[1..3]; // [82, 77]
Array.Sort(scores);
Array.Reverse(scores);

Part 2: List

var names = new List<string> {"Alice","Bob"};
names.Add("Carol");
names.Insert(0,"Zara");
names.Remove("Bob");
names.RemoveAll(n => n.StartsWith("A"));
bool has = names.Contains("Carol");

Part 3: Dictionary

var scores = new Dictionary<string,int>
{
    ["Alice"] = 95,
    ["Bob"] = 82
};
scores["Carol"] = 88;
// Safe access with TryGetValue
if (scores.TryGetValue("Dave", out int s))
    Console.WriteLine($"Dave: {s}");

Part 4: HashSet, Queue & Stack

// HashSet โ€” unique elements, O(1) lookup
var tags = new HashSet<string> {"csharp","dotnet"};
tags.Add("csharp"); // Ignored โ€” already exists

// Queue โ€” FIFO
var q = new Queue<string>();
q.Enqueue("first"); var item = q.Dequeue();

// Stack โ€” LIFO
var stack = new Stack<int>();
stack.Push(1); int top = stack.Pop();

C# in Visual Studio 2026

๐Ÿ“˜ This lesson is part of the book C# in Visual Studio 2026 by Dr. Liew Voon Kiong.

View on Amazon Kindle Edition