kota's memex

System.List<T> is a bit like go's slice type. It is a "resizable array". They are used vastly more than arrays in practice.

initialize

There are many ways to create a new list:

List<int> numbers = new List<int>();

You can use the initializer syntax:

List<int> numbers = new List<int>() { 1, 2, 3 };

Or this really elegant syntax:

List<string> names = ["Dakota", "Anna", "Eugene"];
foreach (var name in names)
{
	Console.WriteLine($"Hello {name.ToUpper()}!");
}

add, remove, count, contains

names.Add("Jazzi");      // Places item at "back" of list.
names.Remove("Dakota");  // First occurrence is removed.
names.RemoveAt(0);       // First item is removed.
names.Clear();           // All items are removed.
names.Count();           // Number of items in the list.
names.Contains("Jazzi"); // Returns true if the item is in the list.
names.IndexOf("Jazzi");  // Returns the item's index or -1.

foreach considerations

You cannot modify the list from inside a foreach. Trying to do so will throw an InvalidOperation exception.

You must use a normal for loop and lookup the item by index.