Coding/Learn/csharp/lists and collections
C# Foundational Course
01.Why C# & .NET Power Modern Software
02.Anatomy of a C# Application
03.Variables & The C# Type System
04.Console I/O: Reading & Writing
05.String Interpolation & Modern Formatting
06.Arithmetic, Math Class & Modulo
07.Conditional Branching: if, else if, and else
08.Logical Operators (&&, ||, !)
09.Switch Statements & Modern Switch Expressions
10.While & Do-While Loops
11.The Standard For Loop & Counting
12.Loop Control: break and continue
13.Methods: Modular, Reusable Code Blocks
14.Optional Parameters, Named Arguments & out/ref
15.Fixed-Size Arrays & Zero-Based Indexing
16.The Sacred Foreach Loop
17.Dynamic Collections: List<T> & Generics
18.Classes & Objects: The OOP Blueprint
19.Auto-Properties & Constructors
20.Capstone: RPG Character & Inventory Engine
Course Certificate0%

Complete all lessons to earn

LearnDynamic Collections: List<T> & Generics
17 / 20 0%
Lesson 17 of 20
5 mins read

Dynamic Collections: List<T> & Generics

PreviousNext Lesson
In Plain English

Fixed-size arrays are rigid when user input or data size is unknown. The generic List<T> class (from System.Collections.Generic) is a dynamic resizable array that automatically expands and contracts as you add or remove items.

The Mental Model

"If an array is a fixed 6-pack egg carton, a List<T> is a magical accordion shopping cart that expands every time you drop another item into it."

Deep Dive: How It Works

using System.Collections.Generic;: Required namespace.

Declaration: List<string> tasks = new List<string>(); or List<int> scores = new();.

.Add(item): Appends an element to the end of the list.

.Remove(item): Removes the first occurrence of an item.

.RemoveAt(index): Removes item at specific index.

.Count: Returns current number of items (equivalent to array .Length).

.Contains(item): Returns true if item exists in the list.

Syntax Blueprint

using System.Collections.Generic;

List<string> list = new List<string>();
list.Add("First");
list.Add("Second");
Console.WriteLine($"Count: {list.Count}");

Dynamic resizable collection with generic type parameter <T>.

PointCore Rules to Remember

Point star
Automatic Resizing: Expands underlying memory buffer automatically as items are added.
Point star
Type-Safe Generics (<T>): List<int> only accepts integers; prevents accidental type corruption.
Point star
The Modern Standard: Used far more frequently than raw fixed-size arrays in modern .NET.

Common Beginner Traps & How to Fix Them

Mistake:Using .Length instead of .Count on a List<T>

Why it happens: Confusion with arrays.

How to fix: Arrays use .Length; Lists and Collections use .Count.

Pro Tip: Initialize lists with collection initializers: var fruits = new List<string> { "Apple", "Banana", "Orange" };!

Live Interactive Example

Hit Run Code to see it live
Managing a Dynamic Inventory with List<T>
C# 12 / .NET
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Output Console
Click "Run Code" to view the rendered output.
How it works: List<T> dynamically managed additions and removals.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Build a numbers list

  • PointCreate a List<int> nums = new List<int>();
  • PointAdd 50 and 100 with .Add().
  • PointPrint "List size: 2".
1
2
3
4
5
6
7
8
9
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates