C# Lists

What is 'List' in C#?

Lists

A list is a data structure that stores a collection of elements in a particular order. It provides methods to add, remove, and access elements within the list. Lists are part of the 'System.Collections.Generic' namespace.

Here's a basic overview of using lists in C#:

Creating a List:

using System.Collections.Generic;
List<int> numbers = new List<int>();

Create a list by specifying the type of elements it will contain.

Adding Elements:

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

Add elements to a list using the 'Add()' method.

Accessing Elements:

int firstElement = numbers[0]; // Accessing the first element

Access elements by index using the bracket notation.

Removing Elements:

numbers.Remove(20); // Remove by value
numbers.RemoveAt(0); // Remove by index

Remove elements by value or index using methods like 'Remove()' or 'RemoveAt()'.

Iterating through a List:

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

Use a loop to iterate through all elements in the list.

Getting the Count:

int count = numbers.Count;

Get the number of elements in the list using the 'Count' property.

Other Operations:

Lists support various other operations such as sorting ('Sort()'), searching ('Contains()'), copying ('CopyTo()'), and more.

Note: Lists in C# are dynamic, meaning they can grow or shrink in size as needed and they provide flexibility and efficiency in managing collections of elements.

A simple C# program that demonstrates the use of lists to store and manipulate a collection of integers:

cs Copy Code
using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        // Create a list to store integers
        List<int> numbers = new List<int>();

        // Add some numbers to the list
        numbers.Add(10);
        numbers.Add(20);
        numbers.Add(30);

        // Print the elements of the list
        Console.WriteLine("Elements in the list:");
        foreach (var number in numbers)
        {
            Console.WriteLine(number);
        }

        // Remove an element from the list
        numbers.Remove(20);

        // Print the elements of the list after removal
        Console.WriteLine("\nElements in the list after removing 20:");
        foreach (var number in numbers)
        {
            Console.WriteLine(number);
        }

        // Accessing elements by index
        Console.WriteLine("\nAccessing element at index 0: " + numbers[0]);

        // Getting the count of elements in the list
        Console.WriteLine("\nNumber of elements in the list: " + numbers.Count);
    }
}
Output:
Elements in the list:
10
20
30

Elements in the list after removing 20:
10
30

Accessing element at index 0: 10       

Number of elements in the list: 2

This program creates a list of integers, adds some numbers to it, removes one element, accesses elements by index, and prints the count of elements in the list.

What's Next?

We've now entered the finance section on this platform, where you can enhance your financial literacy.