Namespace: System.Collections.Generic
List<T>
Represents a strongly typed list of objects that can be accessed by index. Provides methods for manipulating the list.
Table of Contents
Constructors
-
List()public List<T>()
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 the elements.
-
List(int capacity)public List<T>(int capacity)
Initializes a new instance of the List<T> class that is empty and has the specified initial capacity.
capacity: The number of elements that the new list can initially store. -
List(IEnumerable<T> collection)public List<T>(IEnumerable<T> collection)
Initializes a new instance of the List<T> class that contains elements copied from the specified collection and has enough capacity to accommodate the number of elements copied.
collection: The collection whose elements are copied to the new list.
Properties
-
Capacitypublic int Capacity { get; set; }
Gets or sets the number of elements that the List<T> can contain.
-
Countpublic int Count { get; }
Gets the number of elements actually contained in the List<T>.
-
Itempublic T this[int index] { get; set; }
Gets or sets the element at the specified index.
index: The zero-based index of the element to get or set.
Methods
-
Addpublic void Add(T item)
Adds an object to the end of the List<T>.
item: The object to be added to the List<T>. The value can be null for reference types. -
AddRangepublic void AddRange(IEnumerable<T> collection)
Appends the elements of the specified collection to the end of the List<T>.
collection: The collection whose elements should be added to the end of the List<T>. Applications that need to support the collection of ICollection<T> objects should use the parameter type and not the base interface type. -
Clearpublic void Clear()
Removes all elements from the List<T>.
-
Containspublic bool Contains(T item)
Determines whether an element is in the List<T>.
item: The object to locate in the List<T>. The value can be null for reference types.Returns: true if item is found in the List<T>; otherwise, false.
-
Existspublic bool Exists(Predicate<T> match)
Determines whether any element of the current List<T> matches the specified condition.
match: The condition to test for each element. The value of this parameter must not be null.Returns: true if the list contains one or more elements that match the specified condition; otherwise, false.
-
Findpublic T Find(Predicate<T> match)
Searches for an element that matches the specified condition and returns the zero-based index of the first occurrence within the entire List<T>.
match: The condition to test for each element. The value of this parameter must not be null.Returns: The zero-based index of the first occurrence of the element that matches the condition, or -1 if no such element is found.
-
FindAllpublic List<T> FindAll(Predicate<T> match)
Implements the IList<T>.Insert method.
match: The condition to test for each element. The value of this parameter must not be null.Returns: A List<T> containing all the elements that match the specified condition.
-
IndexOfpublic int IndexOf(T item)
Searches for the specified object and returns the zero-based index of the first occurrence within the entire List<T>.
item: The object to locate in the List<T>. The value can be null for reference types.Returns: The zero-based index of the first occurrence of item within the entire List<T>, or -1 if there are no occurrences or the item is not present.
-
Insertpublic void Insert(int index, T item)
Inserts an element into the List<T> at the specified index.
index: The zero-based index at which item should be inserted. item: The object to insert into the List<T>. The value can be null for reference types. -
Removepublic bool Remove(T item)
Removes the first occurrence of a specific object from the List<T>.
item: The object to remove from the List<T>. The value can be null for reference types.Returns: true if item is successfully removed from the List<T>; otherwise, false. This method also returns false if item is not found in the original List<T>.
-
RemoveAtpublic void RemoveAt(int index)
Removes the element at the specified index of the List<T>.
index: The zero-based index of the element to remove. -
Sortpublic void Sort()
Sorts the elements in the entire List<T> using the default comparer.
-
ToArraypublic T[] ToArray()
Copies the entire List<T> to a new one-dimensional Array, using the array's zero-based index.
Returns: A one-dimensional Array containing the elements of the List<T>.
Events
-
This type implements no events.
Interfaces
-
System.Collections.IList<T>
-
System.Collections.Generic.IReadOnlyList<T>
-
System.Collections.ICollection
-
System.Collections.Generic.ICollection<T>
-
System.Collections.IEnumerable
-
System.Collections.Generic.IEnumerable<T>