Represents a strongly typed list of objects that can be accessed by index. Provides methods for creating, searching, and manipulating lists.
Namespace
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
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.
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.
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
Adds an object to the end of the List<T>
.
var numbers = new List<int>(); numbers.Add(1); numbers.Add(2);
Appends the elements of the specified collection to the end of the List<T>
.
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.
Removes all elements from the List<T>
.
Determines whether an element is in the List<T>
.
Converts the elements in the current List<T>
to another type, and then returns a list containing the converted elements.
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>
.
Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire List<T>
.
Executes the specified action on each element of the List<T>
.
Searches for the specified object and returns the zero-based index of the first occurrence within the entire List<T>
.
Inserts an element into the List<T>
at the specified index.
Removes the first occurrence of a specific object from the List<T>
.
Removes the element at the specified index of the List<T>
.
Sorts the elements in the entire List<T>
using the default comparer.
Sorts the elements in the entire List<T>
using the specified comparer.
Properties
Gets or sets the number of elements that the List<T>
can contain.
Gets the number of elements contained in the List<T>
.
Gets or sets the element at the specified index.