.NET API Documentation

List<T> Class

System.Collections.Generic

Represents a strongly typed list of objects that can be accessed by index. Provides methods for manipulating the list as if it were a array.

Remarks

The List<T> generic collection is equivalent to the ArrayList class, but the List<T> generic class uses generics to limit the type of elements that can be stored and to provide better performance.

The List<T> class is the generic implementation of the IList interface. It offers a flexible way to store, retrieve, and manipulate a collection of objects of a specific type. Elements can be added, removed, or accessed by their zero-based index.

List<T> provides optimized methods for common operations such as sorting, searching, and resizing. It automatically manages its capacity as elements are added, resizing the underlying array when necessary.

Type Parameters

T

The type of elements in the list. This type parameter is invariant. This type parameter is contravariant.

Inheritance

Object
    IList<T>
        ICollection<T>
            IEnumerable<T>
                IEnumerable
        ICollection
            IEnumerable
        ICloneable
        IReadOnlyCollection<T>
            IEnumerable<T>
                IEnumerable
    List<T>

Constructors

Constructors

  • List() (T[] collection)
    Initializes a new instance of the List<T> class that is empty and has the default initial capacity.
  • List(int capacity) (int capacity)
    Initializes a new instance of the List<T> class that is empty and has the specified initial capacity.
  • List(IEnumerable<T> collection) (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

Public Methods

  • Add(T item) (T item)
    Adds an object to the end of the List<T>.
  • AddRange(IEnumerable<T> collection) (IEnumerable<T> collection)
    Appends the elements of the specified collection to the end of the List<T>.
  • AsReadOnly() ReadOnlyCollection<T> AsReadOnly()
    Returns a read-only wrapper around the entire List<T>.
  • BinarySearch(T item) (T item)
    Searches the entire sorted List<T> for an element using the default comparer and returns the zero-based index of the element.
  • Clear() void Clear()
    Removes all elements from the List<T>.
  • Contains(T item) (T item)
    Determines whether an element is in the List<T>.
  • Exists(Predicate<T> match) (Predicate<T> match)
    Determines whether any element in the list matches the specified condition.
  • Find(Predicate<T> match) (Predicate<T> match)
    Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence in the entire List<T>.
  • FindAll(Predicate<T> match) (Predicate<T> match)
    Implements the Contains method.
  • FindIndex(Predicate<T> match) (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 in the entire List<T>.
  • ForEach(Action<T> action) (Action<T> action)
    Executes the specified action on each element of the List<T>.
  • GetEnumerator() IEnumerator<T> GetEnumerator()
    Returns an enumerator that iterates through the List<T>.
  • GetRange(int index, int count) (int index, int count)
    Copies a range of elements from the List<T> to a new list.
  • IndexOf(T item) (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) (int index, T item)
    Inserts an element into the List<T> at the specified index.
  • InsertRange(int index, IEnumerable<T> collection) (int index, IEnumerable<T> collection)
    Inserts the elements of a collection into the List<T> at the specified index.
  • Remove(T item) (T item)
    Removes the first occurrence of a specific object from the List<T>.
  • RemoveAll(Predicate<T> match) (Predicate<T> match)
    Removes all the elements that match the conditions defined by the specified predicate.
  • RemoveAt(int index) (int index)
    Removes the element at the specified index of the List<T>.
  • RemoveRange(int index, int count) (int index, int count)
    Removes a range of elements from the List<T>.
  • Reverse() void Reverse()
    Reverses the order of the elements in the entire List<T>.
  • Sort() void Sort()
    Sorts the elements in the entire List<T> using the default comparer.
  • ToArray() T[] ToArray()
    Copies the entire List<T> to a new array.
  • TrimExcess() void 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

Public Properties

  • Capacity int Capacity { get; set; }
    Gets or sets the number of elements that the List<T> can hold without exceeding its capacity.
  • Count int Count { get; }
    Gets the number of elements contained in the List<T>.
  • Item T Item { get; set; }
    Gets or sets the element at the specified index.

Example Usage

// Create a new list of strings
List<string> names = new List<string>();

// Add elements to the list
names.Add("Alice");
names.Add("Bob");
names.Add("Charlie");

// Access an element by index
Console.WriteLine(names[0]); // Output: Alice

// Iterate through the list
foreach (string name in names)
{
    Console.WriteLine(name);
}

// Remove an element
names.Remove("Bob");

// Check if an element exists
if (names.Contains("Alice"))
{
    Console.WriteLine("Alice is in the list.");
}

// Get the count of elements
Console.WriteLine($"Number of names: {names.Count}");