public sealed class List<T> : IList<T>, ICollection<T>, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable

Represents a strongly typed list of objects that can be accessed by index. Provides methods for creating, searching, and manipulating lists.

Namespace

System.Collections.Generic

Assembly

System.Private.CoreLib.dll

Syntax

public sealed class List<T> : IList<T>, ICollection<T>, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable
            

T: The type of elements in the list.

Remarks

The List<T> class is the generic equivalent of the ArrayList class. It supports dynamic array resizing.

A List<T> can hold up to approximately 2 billion elements due to the default array size limit of 0x7FEFFFFF in the framework.

The Count property gets the number of elements actually contained in the List<T>. The capacity of a List<T> is the number of elements that the internal array can hold. Capacity grows automatically as elements are added. If the number of elements exceeds the capacity, the internal array is reallocated and the elements are copied to the new array before the new element is added.

The Capacity property can be set to retrieve a certain number of elements or to reduce the capacity to the current number of elements.

The List<T> is not thread-safe; therefore, it does not guarantee the thread safety of the Add, Remove, Insert, RemoveAt, or other ICollection members, and it does not guarantee thread safety of synchronization (e.g. SyncRoot).

Constructors

public List()

Initializes a new instance of the List<T> class that is empty, has the default initial capacity, and uses the default equality comparer for the type of elements that the list is to contain.

public List(int capacity)

Initializes a new instance of the List<T> class that is empty, has the specified initial capacity, and uses the default equality comparer for the type of elements that the list is to contain.

public List(IEnumerable<T> collection)

Initializes a new instance of the List<T> class that contains elements copied from the specified collection, has sufficient capacity to accommodate the number of elements copied, and uses the default equality comparer for the type of elements that the list is to contain.

Methods

public void Add(T item)

Adds an object to the end of the List<T>.

Example:
var numbers = new List<int>();
numbers.Add(1);
numbers.Add(2);
            
public void AddRange(IEnumerable<T> collection)

Appends the elements of the specified collection to the end of the List<T>.

public int BinarySearch(T item)

Searches the entire sorted List<T> implemented as an array (which must be sorted) for an element using the default comparer and returns the zero-based index of the element.

public void Clear()

Removes all elements from the List<T>.

public bool Contains(T item)

Determines whether an element is in the List<T>.

public List<TOutput> ConvertAll<TOutput>(Converter<T, TOutput> converter)

Converts the elements in the current List<T> to another type, and then returns a list containing the converted elements.

public int FindIndex(Predicate<T> match)

Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the entire List<T>.

public T Find(Predicate<T> match)

Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire List<T>.

public void ForEach(Action<T> action)

Executes the specified action on each element of the List<T>.

public int IndexOf(T item)

Searches for the specified object and returns the zero-based index of the first occurrence within the entire List<T>.

public void Insert(int index, T item)

Inserts an element into the List<T> at the specified index.

public bool Remove(T item)

Removes the first occurrence of a specific object from the List<T>.

public void RemoveAt(int index)

Removes the element at the specified index of the List<T>.

public void Sort()

Sorts the elements in the entire List<T> using the default comparer.

public void Sort(IComparer<T> comparer)

Sorts the elements in the entire List<T> using the specified comparer.

Properties

public int Capacity { get; set; }

Gets or sets the number of elements that the List<T> can contain.

public int Count { get; }

Gets the number of elements contained in the List<T>.

public T this[int index] { get; set; }

Gets or sets the element at the specified index.

Interfaces