C# List


In C#, a List is a dynamic data structure from the System.Collections.Generic namespace that allows you to store a collection of elements. Unlike arrays, a list can grow or shrink in size automatically as you add or remove items, making it a flexible option for working with collections of data.

Key Features of a List in C#

  • Dynamic size: Unlike arrays, lists can automatically adjust their size when elements are added or removed.
  • Generic: Lists are part of the System.Collections.Generic namespace, which means they can be strongly typed to store elements of a specific type (e.g., List<int>, List<string>).
  • Index-based: Similar to arrays, you can access elements by their index.

Defining a List

You can define a list to store elements of any type. The T in List<T> is the type of elements the list will store.

List<T> listName = new List<T>();

For example, a list of integers:

List<int> numbers = new List<int>();

Or a list of strings:

List<string> names = new List<string>();

Adding Elements to a List

You can use the Add() method to add elements to a list:

List<int> numbers = new List<int>(); numbers.Add(10); numbers.Add(20); numbers.Add(30);

Accessing Elements

You can access elements by their index, just like with arrays:

int firstNumber = numbers[0]; // Access the first element Console.WriteLine(firstNumber); // Output: 10

Modifying Elements

You can modify elements by specifying their index:

numbers[1] = 50; // Change the second element to 50

Removing Elements

  • Remove(): Removes the first occurrence of a specific value from the list.

    numbers.Remove(20); // Removes the value 20 from the list
  • RemoveAt(): Removes the element at the specified index.

    numbers.RemoveAt(0); // Removes the first element
  • Clear(): Removes all elements from the list.

    numbers.Clear(); // Clears the entire list

Checking List Size

You can use the Count property to check how many elements are in the list:

int size = numbers.Count; Console.WriteLine(size); // Output: 2 (after removing one element)

Iterating Through a List

You can use a for loop or a foreach loop to iterate through the elements in a list:

foreach (int number in numbers) { Console.WriteLine(number); }

Common List Methods in C#

  • Add(T item): Adds an element to the end of the list.
  • AddRange(IEnumerable<T> collection): Adds the elements of a collection to the end of the list.
  • Insert(int index, T item): Inserts an element at the specified index.
  • Remove(T item): Removes the first occurrence of the specified element.
  • RemoveAt(int index): Removes the element at the specified index.
  • Clear(): Removes all elements from the list.
  • Contains(T item): Determines whether the list contains a specific value.
  • IndexOf(T item): Returns the index of the first occurrence of the specified value.
  • Sort(): Sorts the elements of the list.
  • Reverse(): Reverses the order of the elements in the list.

Example of List in C#

using System; using System.Collections.Generic; public class Program { public static void Main() { // Create a list of integers List<int> numbers = new List<int>(); // Add elements to the list numbers.Add(10); numbers.Add(20); numbers.Add(30); // Print the list Console.WriteLine("Original list:"); foreach (int number in numbers) { Console.WriteLine(number); } // Modify an element numbers[1] = 50; // Remove an element numbers.Remove(30); // Print the modified list Console.WriteLine("\nModified list:"); foreach (int number in numbers) { Console.WriteLine(number); } // Check the size of the list Console.WriteLine("\nList size: " + numbers.Count); } }

Summary

  • List<T> is a dynamic array-like data structure in C# that automatically resizes when elements are added or removed.
  • It supports operations like adding, removing, sorting, and searching for elements.
  • You can create a list for any data type (int, string, custom objects) using the generic List<T> class.