DEV Community

Cover image for C# List
Zafar Urakov
Zafar Urakov

Posted on

C# List

Introduction

List is a dynamic array in C# that provides a collection of elements of the same type. List offers a variety of convenient methods for adding, removing, and accessing elements in the collection. It is a commonly used data structure when working with collections of objects.

Creating a List

To create a new List in C#, you need to use the 'List<>' class, where 'T' is the type of elements you want to store in the list. For example:

List<int> numbers = new List<int>(); // Creating an empty list of integers
Enter fullscreen mode Exit fullscreen mode

Adding Elements

You can add an element to the list using the 'Add()' method. For example:

numbers.Add(10); // Adding the number 10 to the list
numbers.Add(20); // Adding the number 20 to the list
Enter fullscreen mode Exit fullscreen mode

Accessing Elements

To access elements of the list, you can use indexing starting from zero. For example:

int firstNumber = numbers[0]; // Accessing the first element (10)
int secondNumber = numbers[1]; // Accessing the second element (20)
Enter fullscreen mode Exit fullscreen mode

Removing Elements
To remove an element from the list, you can use the 'Remove()' method. For example:

numbers.Remove(10); // Removing the number 10 from the list
Enter fullscreen mode Exit fullscreen mode

Checking for Element Existence

To check if the list contains a specific element, use the 'Contains()' method. For example:

bool containsTwenty = numbers.Contains(20); // Checking if the list contains the number 20
Enter fullscreen mode Exit fullscreen mode

Iterating through the List

To iterate through all elements in the list, use a foreach loop. For example:

foreach (int number in numbers)
{
    Console.WriteLine(number);
}
Enter fullscreen mode Exit fullscreen mode

And so, here is another example to make it clear what 'List' is understood:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // Create a List of strings
        List<string> fruits = new List<string>();

        // Add elements to the List
        fruits.Add("Apple");
        fruits.Add("Banana");
        fruits.Add("Orange");

        // Access elements by index
        string firstFruit = fruits[0]; // "Apple"
        string secondFruit = fruits[1]; // "Banana"

        // Modify an element
        fruits[0] = "Cherry"; // Replace "Apple" with "Cherry"

        // Get the number of elements in the List
        int count = fruits.Count; // 3

        // Check if an element exists in the List
        bool containsBanana = fruits.Contains("Banana"); // true

        // Remove an element
        fruits.Remove("Orange"); // Remove "Orange" from the List

        // Iterate through the List using a foreach loop
        foreach (string fruit in fruits)
        {
            Console.WriteLine(fruit);
        }
        // Output: Cherry, Banana
    }
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

List in C# is a powerful and convenient data structure that allows you to store and manage a collection of elements of the same type. In this documentation, we have covered the fundamental operations with List that will enable you to efficiently work with your data:

Creating a list using the 'List<>' class.
Adding elements to the list using the 'Add()' method.
Accessing elements of the list through indexing.
Removing elements from the list using the 'Remove()' method.
Checking for the existence of elements using the 'Contains()' method.
Iterating through the list using a foreach loop.
With this knowledge, you will be able to create and manage lists in C# with ease, which will be valuable in application development and data processing. I hope that this documentation and examples have helped you better understand how to use List in C#.

I have a lot of interesting code on GitHub

Top comments (0)