Collections
List Add and Remove
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
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}");
}
}
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
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
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
- Start with
appleandpear. - Add the extra fruit
plum. - Remove
pearfrom the list. - Join the remaining fruit names with commas.
| Step | List contents |
| --- | --- |
| start |
apple,pear| | afterAdd|apple,pear,plum| | afterRemove("pear")|apple,plum|
Exercise: ListAddRemove.cs
Add one fruit to a list, remove pear, and print whether removal worked plus the joined list