0Pricing
C# Academy · Lesson

List in Practice

Add, remove, and search.

List in Practice is a free C# Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why List<T>?

List<T> is the everyday dynamic array in C#. It lives in System.Collections.Generic and grows automatically as you add items.

Unlike a plain array, you never set a fixed size up front. It is type-safe: a List<int> only holds int values, caught at compile time.

using System.Collections.Generic;

List<int> scores = new List<int>();
scores.Add(90);
scores.Add(85);

Creating and Initializing

You can fill a list right away using a collection initializer. This is concise and readable.

The compiler turns each entry into an Add call behind the scenes, so the result is identical to adding items one by one.

using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        var fruits = new List<string> { "apple", "pear", "plum" };
        Console.WriteLine(fruits.Count);
    }
}

Indexing and Count

Access elements by zero-based index, just like an array. list[0] is the first item.

Use Count (not Length) to get how many items the list holds. Reading or writing by index is O(1) constant time.

using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        var nums = new List<int> { 10, 20, 30 };
        Console.WriteLine(nums[1]);
        Console.WriteLine(nums.Count);
    }
}

Adding and Inserting

Add appends to the end in amortized O(1) time. AddRange appends many items at once.

Insert(index, item) places an item at a position, shifting everything after it. That shift makes Insert at the front O(n), so prefer adding at the end when you can.

var list = new List<string> { "b", "c" };
list.Insert(0, "a");
list.AddRange(new[] { "d", "e" });
// list is now a, b, c, d, e

Removing Items

Remove(item) deletes the first matching value and returns true if found. RemoveAt(index) deletes by position.

Both shift later elements left, so removal from the middle is O(n). RemoveAll takes a predicate and removes every match in one pass.

using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        var nums = new List<int> { 1, 2, 3, 4, 5 };
        nums.RemoveAll(n => n % 2 == 0);
        Console.WriteLine(string.Join(",", nums));
    }
}

Searching a List

Contains tells you if a value exists; IndexOf gives its position or -1. Both scan linearly, O(n).

If you find yourself searching a large list repeatedly by value, that linear cost adds up. A HashSet or Dictionary may be a better fit.

var names = new List<string> { "Ann", "Bob", "Cy" };
bool hasBob = names.Contains("Bob");   // true
int pos = names.IndexOf("Cy");          // 2

Iterating

A foreach loop is the clearest way to read every element. You can also use a classic for loop when you need the index.

Do not add or remove items inside a foreach over the same list, it throws InvalidOperationException.

using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        var colors = new List<string> { "red", "green", "blue" };
        foreach (var c in colors)
            Console.WriteLine(c);
    }
}

Sorting

Sort() orders the list in place using the default comparer. For custom order, pass a comparison delegate.

Sorting is O(n log n). To get a sorted copy without changing the original, use LINQ's OrderBy, which returns a new sequence.

using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        var nums = new List<int> { 4, 1, 3, 2 };
        nums.Sort();
        Console.WriteLine(string.Join(",", nums));
    }
}

Capacity vs Count

Count is how many items exist; Capacity is how many it can hold before reallocating its internal array.

When the list grows past capacity, it allocates a larger array and copies items, roughly doubling. If you know the final size, pass it to the constructor to avoid repeated copies.

var list = new List<int>(1000); // reserve capacity
for (int i = 0; i < 1000; i++)
    list.Add(i);
// no intermediate reallocations

List as a Method Argument

A List<T> is a reference type. Passing it to a method passes the reference, so changes inside the method affect the caller's list.

If a method only reads, accept IEnumerable<T> or IReadOnlyList<T> to signal you will not modify it.

using System;
using System.Collections.Generic;

class Program {
    static void AddOne(List<int> xs) => xs.Add(1);
    static void Main() {
        var nums = new List<int>();
        AddOne(nums);
        Console.WriteLine(nums.Count); // 1
    }
}

Converting and Copying

ToArray() produces a fixed-size array; new List<T>(other) makes a shallow copy of another sequence.

A shallow copy duplicates the list structure but shares the same element references, so mutating a contained object is visible through both lists.

var original = new List<int> { 1, 2, 3 };
int[] arr = original.ToArray();
var copy = new List<int>(original);

Quick Check

Pick the operation whose cost stands out.

Recap

List<T> is a growable, type-safe array. Index access and Count are O(1); Add at the end is amortized O(1).

Inserting, removing, and searching by value in the middle are O(n) because of shifting or scanning. Reserve capacity when the size is known to avoid reallocations.

Frequently asked questions

Is the “List in Practice” lesson free?

Yes — the full text of “List in Practice” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “List in Practice”?

Add, remove, and search. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “List in Practice” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. List in Practice
  2. Dictionary Lookups
  3. HashSet and Uniqueness
  4. Choosing a Collection
← Back to C# Academy