Mastering List Collections in C#

Introduction

In C#, the List collection is a versatile and widely used data structure that allows you to work with dynamic arrays of objects. It provides various methods and operations to manipulate and query data efficiently. In this article, we'll explore the power of the List collection and demonstrate how to perform common operations on a list, such as adding, removing, sorting, and searching for elements.

Creating and Initializing a List

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // Creating an empty list of integers
        List<int> numbers = new List<int>();

        // Initializing a list with values
        List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" };
    }
}

Adding Elements to a List
 

1. Add Method

You can add elements to a list using the Add method:

List<int> numbers = new List<int>();
numbers.Add(42);
numbers.Add(99);

2. AddRange Method

To add multiple elements at once, you can use the AddRange method:

List<int> numbers = new List<int>();
numbers.AddRange(new List<int> { 1, 2, 3 });

Removing Elements from a List
 

1. Remove Method

To remove a specific element from the list:

List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" };
fruits.Remove("Banana");

2. RemoveAt Method

To remove an element at a specific index:

List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" };
fruits.RemoveAt(1); // Removes "Banana"

3. RemoveAll Method

To remove all elements that match a specific condition:

List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
numbers.RemoveAll(n => n % 2 == 0); // Removes even numbers

Sorting a List

You can sort a list using the Sort method:

List<int> numbers = new List<int> { 5, 2, 8, 1, 3 };
numbers.Sort(); // Sorts in ascending order

To sort in descending order, you can use a custom comparer:

List<int> numbers = new List<int> { 5, 2, 8, 1, 3 };
numbers.Sort((a, b) => b.CompareTo(a)); // Sorts in descending order

Searching for Elements in a List
 

1. Contains Method

To check if a list contains a specific element:

List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" };
bool containsBanana = fruits.Contains("Banana");

2. Find Method

To find the first element that matches a condition:

List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
int firstEven = numbers.Find(n => n % 2 == 0); // Finds the first even number

3. FindAll Method

To find all elements that match a condition:

List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
List<int> evenNumbers = numbers.FindAll(n => n % 2 == 0); // Finds all even numbers

Conclusion

The List collection in C# is a powerful tool for managing dynamic arrays of data. Whether you're adding, removing, sorting, or searching for elements, the List provides a wide range of methods to streamline these operations. Mastering the List is fundamental for any C# developer, as it forms the backbone of many data manipulation tasks in the language.


Recommended Free Ebook
Similar Articles