Thuta Learning
AdvancedProgrammingbeginner

Collections (List)

Relax. We'll talk through this in plain words — no textbook voice.

List<T> is a collection whose size can change. Arrays are fixed-size, but with a List you can add, remove, and search for items much more easily. When you don't know ahead of time how many items you'll have, List is the go-to choice in real projects.

csharp
using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        List<string> tasks = new List<string>();

        tasks.Add("Learn C# basics");
        tasks.Add("Practice methods");
        tasks.Add("Build a mini project");

        tasks.Remove("Practice methods");

        foreach (string task in tasks)
        {
            Console.WriteLine(task);
        }
    }
}

What you'll learn from this code

  • using System.Collections.Generic; needs to be added before you can use List<T>.
  • Add() adds an item.
  • Remove() removes an item.
  • foreach loops through every item in the list.
You should see
Learn C# basics Build a mini project