List<T> Class
Represents a strongly typed list of objects that can be accessed by index. Provides methods for searching, sorting, and manipulating lists.
Inheritance
Object
→ ICollection<T>
→ IEnumerable<T>
→ IList<T>
→ List<T>
Syntax
public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
Type Parameters
T
: The type of elements in the list.
Constructors
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 values that will be stored in the list.
public List<T> ();
List<T>(Int32)
Initializes a new instance of the List<T>
class that is empty and has the specified initial capacity or contains the elements of the specified collection and uses the default equality comparer for the type of values that will be stored in the list.
public List<T> (int capacity);
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.
public List<T> (IEnumerable<T> collection);
Properties
Capacity
Gets or sets the number of elements that the List<T>
can contain.
public int Capacity { get; set; }
Count
Gets the number of elements actually contained in the List<T>
.
public int Count { get; }
Methods
Add(T)
Adds an object to the end of the List<T>
.
public void Add (T item);
AddRange(IEnumerable<T>)
Appends the elements of the specified collection to the end of the List<T>
.
public void AddRange (IEnumerable<T> collection);
Clear()
Removes all elements from the List<T>
.
public void Clear ();
Contains(T)
Determines whether an element is in the List<T>
.
public bool Contains (T item);
Find(Predicate<T>)
Searches the entire sorted List<T>
for an element using the default equality comparer and returns the zero-based index of the element.
public T Find (Predicate<T> match);
IndexOf(T)
Searches for the specified object and returns the zero-based index of the first occurrence within the entire List<T>
.
public int IndexOf (T item);
Insert(Int32, T)
Inserts an element into the List<T>
at the specified index.
public void Insert (int index, T item);
Remove(T)
Removes the first occurrence of a specific object from the List<T>
.
public bool Remove (T item);
RemoveAt(Int32)
Removes the element at the specified index of the List<T>
.
public void RemoveAt (int index);
Reverse()
Reverses the order of the elements in the entire List<T>
.
public void Reverse ();
Sort()
Sorts the elements in the entire List<T>
using the default comparer.
public void Sort ();
Example
C# 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("Second fruit: " + fruits[1]); // Output: Banana
// Check if an element exists
if (fruits.Contains("Cherry"))
{
Console.WriteLine("Cherry is in the list.");
}
// Iterate through the list
Console.WriteLine("\nAll fruits:");
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
// Remove an element
fruits.Remove("Banana");
// Sort the list
fruits.Sort();
Console.WriteLine("\nSorted fruits after removing Banana:");
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
// Clear the list
fruits.Clear();
Console.WriteLine($"\nList count after clearing: {fruits.Count}");
}
}
Note: The List<T>
class is a generic collection and provides type safety. It is generally preferred over non-generic collections like ArrayList
when working with a collection of a specific type.
Important: Modifying a collection while iterating over it using a standard foreach
loop can lead to exceptions. Use caution when removing or adding elements during iteration.