List<T>Represents a strongly typed list of objects that can be accessed by index. Provides methods for manipulating the list.
System.Collections.GenericSystem.Collections.dllThe List<T> generic collection is the programmatic representation of a dynamic array. It can hold objects of any type, from any non-private code. The size of the List<T> is automatically increased as elements are added.
Each item in the list is an object. You can access any member of the item programmatically as long as you specify the correct index of the item.
The List<T> class provides performance that is similar to the ArrayList class. It is generally recommended to use the List<T> generic collection when programming in C# because it provides better performance and type safety.
public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
List()List<T> class that is empty, has the default initial capacity, and uses the default equality comparer for the type of elements that will be stored in the list.List(Int32 capacity)List<T> class that is empty, has the specified initial capacity, and uses the default equality comparer for the type of elements that will be stored in the list.List(IEnumerable<T> collection)List<T> class that contains elements copied from the specified collection and has enough available space to accommodate the number of elements copied.CapacityList<T> can contain.public Int32 Capacity { get; set; }
CountList<T>.public Int32 Count { get; }
Item[Int32 index]public T Item[Int32 index] { get; set; }
Add(T item)List<T>.public void Add(T item)
AddRange(IEnumerable<T> collection)List<T>.public void AddRange(IEnumerable<T> collection)
AsReadOnly()List<T>.public ReadOnlyCollection<T> AsReadOnly()
BinarySearch(T item)List<T> for an element using the default comparer and returns the zero-based index of the element.public Int32 BinarySearch(T item)
Clear()List<T>.public void Clear()
Contains(T item)List<T>.public bool Contains(T item)
Exists(Predicate<T> match)List<T> matches the specified predicate.public bool Exists(Predicate<T> match)
Find(Predicate<T> match)List<T>.public T Find(Predicate<T> match)
FindAll(Predicate<T> match)public List<T> FindAll(Predicate<T> match)
ForEach(Action<T> action)List<T>.public void ForEach(Action<T> action)
GetRange(Int32 index, Int32 count)List<T> to a new list.public List<T> GetRange(Int32 index, Int32 count)
IndexOf(T item)List<T>.public Int32 IndexOf(T item)
Insert(Int32 index, T item)List<T> at the specified index.public void Insert(Int32 index, T item)
InsertRange(Int32 index, IEnumerable<T> collection)List<T> at the specified index.public void InsertRange(Int32 index, IEnumerable<T> collection)
LastIndexOf(T item)List<T>.public Int32 LastIndexOf(T item)
Remove(T item)List<T>.public bool Remove(T item)
RemoveAll(Predicate<T> match)public Int32 RemoveAll(Predicate<T> match)
RemoveAt(Int32 index)List<T>.public void RemoveAt(Int32 index)
RemoveRange(Int32 index, Int32 count)List<T>.public void RemoveRange(Int32 index, Int32 count)
Reverse()List<T>.public void Reverse()
Sort()List<T> using the default comparer.public void Sort()
ToArray()List<T> to a new array.public T[] ToArray()
TrimExcess()List<T>, if that number is less than 90 percent of the current capacity.public void TrimExcess()
using System;
using System.Collections.Generic;
List<string> names = new List<string>();
names.Add("Alice");
names.Add("Bob");
names.Add("Charlie");
Console.WriteLine("Elements:");
foreach (string name in names)
{
Console.WriteLine(name);
}
Console.WriteLine($"Element at index 1: {names[1]}"); // Output: Bob
using System;
using System.Collections.Generic;
List<int> numbers = new List<int> { 10, 20, 30, 40, 50 };
numbers.Remove(30); // Removes the first occurrence of 30
numbers.RemoveAt(0); // Removes the element at index 0 (which is 10)
Console.WriteLine("Updated numbers:");
foreach (int num in numbers)
{
Console.WriteLine(num); // Output: 20, 40, 50
}
The List<T> class implements the generic IList<T>, ICollection<T>, and IEnumerable<T> interfaces, and also the non-generic IList, ICollection, and IEnumerable interfaces.
If the capacity of the List<T> is insufficient to accommodate the new element, the elements are copied to a new underlying array before the new element is added.
Thrown when a collection is passed to a constructor or method that does not allow null references.
Thrown when an index is outside the valid range of the list.
Thrown when an operation is attempted that is not supported by the list (e.g., modifying a read-only list).