Skip to content
dreamcode
dreamcode
Map
Lists
Lesson 7 of 21
+15 XP on finish
C# LOOPS & ARRAYSChapter 2 · C# Loops & Arrays

Dynamic generic lists

The List<T> class from System.Collections.Generic represents a strongly-typed list of objects. Unlike arrays, a list grows dynamically as elements are added.

Worked example
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        List<string> stars = new List<string>();
        stars.Add("Sirius");
        stars.Add("Vega");
        Console.WriteLine(stars.Count); // 2
        stars.Remove("Vega");
    }
}

How it reads

  • List<string> defines a list containing strings
  • stars.Add(...) appends a string to the end of the list
  • stars.Count returns the current number of elements
Cloud tip: The <T> syntax is a generic. You specify the type of elements inside the angle brackets, ensuring type safety.

Check your understanding

0 / 2

Answer all 2 to complete this lesson and earn 15 XP.

  1. 1. Which namespace does List<T> live in?
  2. 2. Which property gets the number of elements in a List?
Answer every question to unlock the next lesson.