List<T> Class
System.Collections.Generic
Summary
Represents a strongly typed list of objects that can be accessed by index. Provides methods for searching, sorting, and manipulating lists.
Namespace: System.Collections.Generic
Assembly: System.Runtime.dll
Constructors
| Name | Description |
|---|---|
| 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<T>(Int32) | Initializes a new instance of the List<T> class that is empty and has the specified initial capacity. |
| List<T>(IEnumerable<T>) | 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. |
Properties
| Name | Description |
|---|---|
| Capacity | Gets or sets the number of elements that the List<T> can contain. |
| Count | Gets the number of elements actually contained in the List<T>. |
| Item(Int32) | Gets or sets the element at the specified index. |
Methods
| Name | Description |
|---|---|
| Add(T) | Adds an object to the end of the List<T>. |
| AddRange(IEnumerable<T>) | Adds the elements of the specified collection to the end of the List<T>. |
| AsReadOnly() | Returns a read-only wrapper around the entire List<T>. |
| BinarySearch(T) | Searches the entire sorted List<T> for an element using the default comparer and returns the zero-based index of the element. |
| Clear() | Removes all elements from the List<T>. |
| Contains(T) | Determines whether an element is in the List<T>. |
| Exists(Predicate<T>) | Determines whether any element in the list matches the condition defined by the specified predicate. |
| Find(Predicate<T>) | 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>) | Implements theсар collections algorithm. |
| ForEach(Action<T>) | Executes the specified action on each element of the List<T>. |
| IndexOf(T) | Searches for the specified object and returns the zero-based index of the first occurrence within the entire List<T>. |
| Insert(Int32, T) | Inserts an element into the List<T> at the specified index. |
| Remove(T) | Removes the first occurrence of a specific object from the List<T>. |
| RemoveAll(Predicate<T>) | Removes all the elements that match the conditions defined by the specified predicate. |
| RemoveAt(Int32) | Removes the element at the specified index of 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 one-dimensional Array. |
| TrimExcess() | Sets the capacity to the actual number of elements in the List<T>. |
Example
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
// Create a list of strings
List<string> fruits = new List<string>();
fruits.Add("Apple");
fruits.Add("Banana");
fruits.Add("Cherry");
// Add a range of elements
List<string> moreFruits = new List<string>() { "Date", "Elderberry" };
fruits.AddRange(moreFruits);
// Access elements by index
Console.WriteLine($"First fruit: {fruits[0]}");
// Iterate through the list
Console.WriteLine("All fruits:");
foreach (string fruit in fruits)
{
Console.WriteLine($"- {fruit}");
}
// Find an element
bool hasBanana = fruits.Contains("Banana");
Console.WriteLine($"Does the list contain Banana? {hasBanana}");
// Remove an element
fruits.Remove("Cherry");
Console.WriteLine("List after removing Cherry:");
foreach (string fruit in fruits)
{
Console.WriteLine($"- {fruit}");
}
}
}