List<T> Class

System.Collections.Generic

Represents a strongly typed list of objects that can be accessed by index. Provides methods for manipulating lists by adding, removing, sorting, and searching elements. Supports dynamic resizing.

Remarks

The List<T> generic collection is the .NET Framework implementation of a dynamically resizable array. It is an improvement over the ArrayList collection, which does not enforce type safety. By using List<T>, you can prevent runtime errors that might occur when you add an element of the wrong type to the ArrayList.

The capacity of a List<T> is the number of elements the list can hold. As elements are added to a List<T>, the capacity is automatically increased by reallocating the internal array. If all the slots in the internal array are filled, the entire internal array is reallocated to accommodate the new element. The capacity doubles by default.

If you are using List<T> and you want to reduce the capacity of the list to a specific value, use the TrimExcess() method.

Constructors

  • List<T>()
    Initializes a new instance of the List<T> class that is empty, has the default initial capacity, and contains no elements.
  • List<T>(Int32)
    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<T>(IEnumerable<T>)
    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.

    collection: The collection whose elements are copied to the new list.

Methods

  • Add(T)
    Adds an object to the end of the List<T>.

    item: The object to be added to the end of the List<T>. The value can be null for reference types.

  • AddRange(IEnumerable<T>)
    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>. The elements are added in the order they are returned by the enumerator.

  • Clear()
    Removes all elements from the List<T>.
  • Contains(T)
    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.

  • FindIndex(Predicate<T>)
    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>.

    match: The Predicate<T> delegate that defines the conditions of the element to search for.

    Returns: The zero-based index of the first occurrence of the element that matches the conditions defined by match, if found; otherwise, -1.

  • Insert(Int32, T)
    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. The value can be null for reference types.

  • Remove(T)
    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 was successfully removed from the List<T>; otherwise, false. This method also returns false if item is not found in the original List<T>.

  • RemoveAt(Int32)
    Removes the element at the specified index of the List<T>.

    index: The zero-based index of the element to remove.

  • Sort()
    Sorts the elements in the entire List<T> using the default comparer.
  • Sort(Comparison<T>)
    Sorts the elements in the entire List<T> using the specified comparer.

    comparison: The Comparison<T> delegate that defines the comparison to use when sorting elements. Use null to use the default comparer.

Properties

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

    Returns: The number of elements that the List<T> can currently store.

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

    Returns: The number of elements contained in the List<T>.

  • Item[Int32]
    Gets or sets the element at the specified index.

    index: The zero-based index of the element to get or set.

    Returns: The element at the specified index. Setting the value replaces the element at the specified index.

Example

The following example demonstrates how to create, populate, and manipulate a List<T>.


using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a list of strings
        List<string> names = new List<string>();

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

        Console.WriteLine("Initial list:");
        foreach (string name in names)
        {
            Console.WriteLine(name);
        }

        // Insert an element
        names.Insert(1, "David");
        Console.WriteLine("\nList after insertion:");
        foreach (string name in names)
        {
            Console.WriteLine(name);
        }

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

        // Remove an element
        names.Remove("Charlie");
        Console.WriteLine("\nList after removing Charlie:");
        foreach (string name in names)
        {
            Console.WriteLine(name);
        }

        // Sort the list
        names.Sort();
        Console.WriteLine("\nSorted list:");
        foreach (string name in names)
        {
            Console.WriteLine(name);
        }

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

        // Clear the list
        names.Clear();
        Console.WriteLine($"\nNumber of elements after clearing: {names.Count}");
    }
}