Skip to content
CShift0.32

List<T>

Struct · namespace System

struct List<T>

A growable array.

using System;
var names = List<string>.Create();
names.Add("Ann");
names.Add("Bob");
foreach (var name in names)
Console.WriteLine(name);

A List is a small handle to shared storage: copies of a List (assignments, arguments) see the same elements, like a reference. The storage is made by Create() or a collection expression ([]); the zero value (new(), a field without a value) is an empty list that can be read but not changed: Add panics.

list[i] reads an element (List<T>.Get), list[i] = x writes one (List<T>.Set); foreach goes through the elements in order.

Name Description
Create A new, empty list.
static List<T> Create()

A new, empty list.

static List<T> Create(int capacity)

A new, empty list with room for capacity elements before it has to grow.

Name Description
IsCreated Whether the list has storage: made by Create() or [], not the zero value (which cannot change).
Count The number of elements.
Capacity Number of elements that fit without reallocating.
Get The element at index (also list[index]).
Set Replaces the element at index (also list[index] = value).
Add Adds value at the end.
AddRange Adds all elements of values at the end, in order.
Insert Inserts value before the element at index; index == Count() appends.
RemoveAt Removes the element at index; the elements after it move down by one.
Clear Removes all elements.
IndexOf The index of the first element that equals value (IEquatable).
Contains Whether an element equals value (IEquatable).
Remove Removes the first element that equals value (IEquatable).
Reverse Reverses the order of the elements.
Sort Sorts the elements in ascending order (IComparable).
ToArray A new array with the elements, in order.
ForEach Calls action for every element, in order: list.ForEach(x => Console.WriteLine(x)).
Where A new list with the elements for which keep returns true: list.Where(x => x > 0).
Select A new list with convert applied to every element: list.Select<string>(x => x.ToString()).
Any Whether test returns true for at least one element.
All Whether test returns true for every element (true for an empty list).
FindIndex The index of the first element for which ‘test’ is true, or -1.
bool IsCreated()

Whether the list has storage: made by Create() or [], not the zero value (which cannot change).

Since 0.26

int Count()

The number of elements.

int Capacity()

Number of elements that fit without reallocating.

T Get(int index)

The element at index (also list[index]).

Panics when index is not in 0 to Count() - 1.

void Set(int index, T value)

Replaces the element at index (also list[index] = value).

Panics when index is not in 0 to Count() - 1.

void Add(T value)

Adds value at the end.

Panics when the list was not created (List<T>.IsCreated).

void AddRange(T[] values)

Adds all elements of values at the end, in order.

void Insert(int index, T value)

Inserts value before the element at index; index == Count() appends.

Panics when index is not in 0 to Count().

void RemoveAt(int index)

Removes the element at index; the elements after it move down by one.

Panics when index is not in 0 to Count() - 1.

void Clear()

Removes all elements.

int IndexOf(T value) where T : IEquatable<T>

The index of the first element that equals value (IEquatable).

Returns -1 if no element equals it.

bool Contains(T value) where T : IEquatable<T>

Whether an element equals value (IEquatable).

bool Remove(T value) where T : IEquatable<T>

Removes the first element that equals value (IEquatable).

Returns whether an element was removed.

void Reverse()

Reverses the order of the elements.

void Sort() where T : IComparable<T>

Sorts the elements in ascending order (IComparable). The sort is stable: equal elements keep their order.

T[] ToArray()

A new array with the elements, in order.

void ForEach(Action<T> action)

Calls action for every element, in order: list.ForEach(x => Console.WriteLine(x)).

Since 0.04

List<T> Where(Func<T, bool> keep)

A new list with the elements for which keep returns true: list.Where(x => x > 0).

Since 0.04

List<U> Select<U>(Func<T, U> convert)

A new list with convert applied to every element: list.Select<string>(x => x.ToString()).

Since 0.04

bool Any(Func<T, bool> test)

Whether test returns true for at least one element.

Since 0.04

bool All(Func<T, bool> test)

Whether test returns true for every element (true for an empty list).

Since 0.04

int FindIndex(Func<T, bool> test)

The index of the first element for which ‘test’ is true, or -1.

Since 0.04