List<T> Class
Represents a strongly typed list of objects that can be accessed by index. Provides methods for searching, sorting, and manipulating lists.
Overview
The List<T>
class is a generic collection that provides an ordered list of elements. It is highly flexible and efficient for many common programming tasks. Because the List<T>
generic class allows you to store any type of object, you can avoid the cost of casting to and from object
when you need to retrieve values from the list.
Syntax
public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
Type Parameters
T
: The type of elements in the list. This type parameter is not constrained. When you use List<T>
, you specify the type of elements the list will hold.
Fields
The List<T>
class does not expose any public fields.
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 will be stored in the list. |
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 will be stored in the list. |
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. |
Properties
Name |
Description |
Capacity |
Gets or sets the number of elements that the List<T> can contain at one time. |
Count |
Gets the number of elements actually contained in the List<T> . |
Item[int index] |
Gets or sets the element at the specified index. |
Methods
Adding Elements
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> . |
Insert(int index, T item) |
Inserts an element into the List<T> at the specified zero-based index. |
InsertRange(int index, IEnumerable<T> collection) |
Inserts the elements of a collection into the List<T> at the specified zero-based index. |
Removing Elements
Name |
Description |
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> . |
RemoveAll(Predicate<T> match) |
Removes all the elements that match the conditions defined by the specified predicate. |
RemoveRange(int index, int count) |
Removes a range of elements from the List<T> . |
Searching and Sorting
Name |
Description |
Contains(T item) |
Determines whether an element is in 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> . |
FindIndex(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> . |
Sort() |
Sorts the elements in the entire List<T> using the default comparer. |
Sort(Comparison<T> comparison) |
Sorts the elements in the entire List<T> using the specified Comparison<T> . |
Other Methods
Name |
Description |
Clear() |
Removes all elements from the List<T> . |
CopyTo(T[] array, int arrayIndex) |
Copies the entire List<T> to a compatible one-dimensional Array , starting at the specified index of the target array. |
Reverse() |
Reverses the order of the elements in the entire List<T> . |
Example Usage
The following example demonstrates how to create a List<string>
, add elements, access elements by index, and remove elements.
using System;
using System.Collections.Generic;
public class Example
{
public static void Main(string[] args)
{
// Create a list of strings
List<string> fruits = new List<string>();
// Add elements
fruits.Add("Apple");
fruits.Add("Banana");
fruits.Add("Orange");
// Add multiple elements using AddRange
fruits.AddRange(new string[] { "Grape", "Mango" });
// Access element by index
Console.WriteLine($"Element at index 1: {fruits[1]}"); // Output: Banana
// Iterate through the list
Console.WriteLine("\nAll fruits:");
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
// Remove an element
fruits.Remove("Orange");
// Remove element at a specific index
fruits.RemoveAt(0); // Removes "Apple"
Console.WriteLine("\nFruits after removal:");
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
// Check if an element exists
if (fruits.Contains("Grape"))
{
Console.WriteLine("\nGrape is in the list.");
}
// Get the count
Console.WriteLine($"\nNumber of fruits: {fruits.Count}");
}
}