List<T>
In this page:
List<T>
List<T> is a dynamically resizable collection from System.Collections.Generic, unlike fixed-size arrays. You can add, remove, and search elements, and it grows automatically as needed. It's the most commonly used general-purpose collection in C#.
Note: Prefer List<T> over arrays whenever the number of elements can change at runtime.
Example: List<T>
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
List<string> names = new List<string> { "Amy", "Bo" };
names.Add("Cy");
names.Remove("Bo");
Console.WriteLine(names.Count);
foreach (string name in names)
{
Console.WriteLine(name);
}
}
}
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: