Collections
List Add and Remove
List<T> can grow as items are added and shrink as items are removed.
List Add and Remove
ListAddRemove.cs
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
string extra = ;
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 = ;
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 = ;
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}");
}
}
list
A list is an ordered collection that can change size.