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.
Static methods
Section titled “Static methods”| Name | Description |
|---|---|
Create |
A new, empty list. |
Create
Section titled “Create”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.
Methods
Section titled “Methods”| 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. |
IsCreated
Section titled “IsCreated”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.
Capacity
Section titled “Capacity”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).
AddRange
Section titled “AddRange”void AddRange(T[] values)Adds all elements of values at the end, in order.
Insert
Section titled “Insert”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().
RemoveAt
Section titled “RemoveAt”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.
IndexOf
Section titled “IndexOf”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.
Contains
Section titled “Contains”bool Contains(T value) where T : IEquatable<T>Whether an element equals value (IEquatable).
Remove
Section titled “Remove”bool Remove(T value) where T : IEquatable<T>Removes the first element that equals value (IEquatable).
Returns whether an element was removed.
Reverse
Section titled “Reverse”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.
ToArray
Section titled “ToArray”T[] ToArray()A new array with the elements, in order.
ForEach
Section titled “ForEach”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
Select
Section titled “Select”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
FindIndex
Section titled “FindIndex”int FindIndex(Func<T, bool> test)The index of the first element for which ‘test’ is true, or -1.
Since 0.04