List<T> Class
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 (in System.Private.CoreLib.dll)
Summary
The List<T> generic collection provides an array-like structure with the added convenience of being able to dynamically resize itself as elements are added or removed.
List<T> implements the IList<T>, ICollection<T>, and IEnumerable<T> generic interfaces. It also implements the non-generic interfaces IList, ICollection, and IEnumerable.
Remarks
The capacity of a List<T> is the number of elements that the List<T> can hold. As elements are added to a List<T>, the capacity is automatically increased by reallocating the internal array. If all the elements are removed from a List<T>, the capacity will be reset to zero.
Elements can be accessed using an integer index, which ranges from zero to the number of elements in the list minus one.
Constructors
| Name | Description |
|---|---|
| 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. |
| 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. |
| List(IEnumerable<T> collection) | Initializes a new instance of the List<T> class that contains elements copied from the specified collection and has sufficient capacity to accommodate the number of elements copied. |
Methods
| Name | Description |
|---|---|
| Add(T item) | Adds an object to the end of the List<T>. |
| AddRange(IEnumerable<T> collection) | Adds the elements of the specified collection to the end of the List<T>. |
| Clear() | Removes all elements from the List<T>. |
| Contains(T item) | Determines whether an element is in the List<T>. |
| Exists(Predicate<T> match) | Determines whether any element in the list matches the specified condition. |
| Find(Predicate<T> match) | Searches for an element that matches the specified condition and returns the first occurrence in the entire List<T>. |
| FindAll(Predicate<T> match) | Creates a shallow copy of the elements that match the specified condition. |
| FindIndex(Predicate<T> match) | Searches for an element that matches the specified condition and returns the zero-based index of the first occurrence in the entire List<T>. |
| ForEach(Action<T> action) | Executes the specified action on each element of the List<T>. |
| IndexOf(T item) | Searches for the specified object and returns the zero-based index of the first occurrence within the entire List<T>. |
| Insert(int index, T item) | Inserts an element into the List<T> at the specified zero-based index. |
| Remove(T item) | Removes the first occurrence of a specific object from the List<T>. |
| RemoveAt(int index) | Removes the element at the specified index of the List<T>. |
| RemoveRange(int index, int count) | Removes a range of elements from the List<T>. |
| Reverse() | Reverses the order of the elements in the entire List<T>. |
| Sort() | Sorts the elements in the entire List<T> using the default comparer. |
| ToArray() | Copies the entire List<T> to a new array. |
| TrimExcess() | Sets the capacity to the actual number of elements in the List<T>, if that number is less than 90 percent of the current capacity. |
Properties
| Name | Description |
|---|---|
| Capacity | Gets or sets the number of elements that the List<T> can contain. |
| Count | Gets the number of elements contained in the List<T>. |
| Item[int index] | Gets or sets the element at the specified index. |
Example Usage
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
// Create a list of strings
List<string> names = new List<string>();
names.Add("Alice");
names.Add("Bob");
names.Add("Charlie");
// Access an element
Console.WriteLine("First name: " + names[0]); // Output: First name: Alice
// Iterate through the list
Console.WriteLine("All names:");
foreach (string name in names)
{
Console.WriteLine(name);
}
// Remove an element
names.Remove("Bob");
// Add a range of elements
List<string> moreNames = new List<string> { "David", "Eve" };
names.AddRange(moreNames);
// Get the count
Console.WriteLine("Number of names: " + names.Count);
}
}