List<T> can grow as items are added and shrink as items are removed.

list A list is an ordered collection that can change size.

List Add and Remove

extra
ListAddRemove.cs
Replay: real traced execution (multi-file project)
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        string extra = "plum";
        List<string> fruits = new List<string> { "apple", "pear" };
        fruits.Add(extra);
        bool removed = fruits.Remove("pear");
        string joined = string.Join(",", fruits);

        Console.WriteLine($"extra={extra}");
        Console.WriteLine($"removed={removed}");
        Console.WriteLine($"joined={joined}");
    }
}
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        string extra = "kiwi";
        List<string> fruits = new List<string> { "apple", "pear" };
        fruits.Add(extra);
        bool removed = fruits.Remove("pear");
        string joined = string.Join(",", fruits);

        Console.WriteLine($"extra={extra}");
        Console.WriteLine($"removed={removed}");
        Console.WriteLine($"joined={joined}");
    }
}
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        string extra = "mango";
        List<string> fruits = new List<string> { "apple", "pear" };
        fruits.Add(extra);
        bool removed = fruits.Remove("pear");
        string joined = string.Join(",", fruits);

        Console.WriteLine($"extra={extra}");
        Console.WriteLine($"removed={removed}");
        Console.WriteLine($"joined={joined}");
    }
}
  1. extra ← plum, removed ← True, joined ← apple,plum

    5{6    static void Main()7    {8        string extra→ plum = "plum"; //@extra="kiwi", "mango"9        List<string> fruits = new List<string> { "apple", "pear" };10        fruits.Add(extraplum);11        bool removed→ True = fruits.Remove("pear");12        string joined→ apple,plum = string.Join(",", fruits);1314        Console.WriteLine($"extra={extraplum}");15        Console.WriteLine($"removed={removedTrue}");16        Console.WriteLine($"joined={joinedapple,plum}");17    }
    outputextra=plum
    removed=True
    joined=apple,plum
  1. extra ← kiwi, removed ← True, joined ← apple,kiwi

    5{6    static void Main()7    {8        string extra→ kiwi = "kiwi";9        List<string> fruits = new List<string> { "apple", "pear" };10        fruits.Add(extrakiwi);11        bool removed→ True = fruits.Remove("pear");12        string joined→ apple,kiwi = string.Join(",", fruits);1314        Console.WriteLine($"extra={extrakiwi}");15        Console.WriteLine($"removed={removedTrue}");16        Console.WriteLine($"joined={joinedapple,kiwi}");17    }
    outputextra=kiwi
    removed=True
    joined=apple,kiwi
  1. extra ← mango, removed ← True, joined ← apple,mango

    5{6    static void Main()7    {8        string extra→ mango = "mango";9        List<string> fruits = new List<string> { "apple", "pear" };10        fruits.Add(extramango);11        bool removed→ True = fruits.Remove("pear");12        string joined→ apple,mango = string.Join(",", fruits);1314        Console.WriteLine($"extra={extramango}");15        Console.WriteLine($"removed={removedTrue}");16        Console.WriteLine($"joined={joinedapple,mango}");17    }
    outputextra=mango
    removed=True
    joined=apple,mango

Grow, Then Remove

  1. Start with apple and pear.
  2. Add the extra fruit plum.
  3. Remove pear from the list.
  4. Join the remaining fruit names with commas. | Step | List contents | | --- | --- | | start | apple,pear | | after Add | apple,pear,plum | | after Remove("pear") | apple,plum |

Exercise: ListAddRemove.cs

Add one fruit to a list, remove pear, and print whether removal worked plus the joined list