Are you ready to supercharge your C# game? Lists are an incredible data structure that will take your code to the next level. They did this for me when I started to solve complex problems with Lists in C#.

Why Use Lists in C#?

At its core, a list is a collection of objects that you can store and manipulate in your program. Unlike arrays, lists in C# are more flexible, allowing you to add, remove, and sort items on the fly. Lists can store any object type, making them incredibly versatile for various scenarios.

How to Use Lists in C#?

You’ll need to include the System.Collections.Generic namespace in your program to include a list in your program. From there, you can declare a new list using the following syntax:

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

In this syntax, replace T with the data type you want to store in your list and listName with a name of your choice. For example, to create a list of integers, you could use the following code:

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

Once you have created your list, you can add items to it using the Add() method, like so:

numbers.Add(10);
numbers.Add(20);
numbers.Add(30);

You can use an index to access items in the list, just like with an array. The first item in the list has an index of 0, and the second has an index of 1, and so on. For example, to access the first item in the numbers list, you could use the following code:

int firstNumber = numbers[0];

You can also remove items from a list using the Remove() method, like this:

numbers.Remove(20);

Example: Storing and Manipulating a List of Names

Let’s explore an example of using lists in C# to store and manipulate a list of names. Here’s the code:

using System;
using System.Collections.Generic;
class Program
{
    static void Main(string[] args)
    {
        List<string> names = new List<string>();
        names.Add("John");
        names.Add("Doe");
        names.Add("Wick");
        Console.WriteLine("List of Names:");
        foreach (string name in names)
        {
            Console.WriteLine(name);
        }
        names.Remove("Doe");
        Console.WriteLine("\nList of Names after removing Doe:");
        foreach (string name in names)
        {
            Console.WriteLine(name);
        }
    }
}

In this example, you create a new list of strings called names and add three names: John, Doe, and Wick. After that, you use a foreach loop to print out the list of names. Then remove the name “Doe” using the Remove() method and print out the updated list using another foreach loop. Execute this code, and you will have the following output:

Master Lists in C# - A Beginner's Guide | naveedulhaq.com

Well, that sum’s up almost everything about Lists in C#

Keep Learning and Experimenting

Now that you understand how lists work in C#, it’s time to experiment with them yourself. Try creating lists of different data types and exploring the many methods for manipulating and sorting them. With enough practice, you’ll become a master of lists in no time.

Categorized in: