A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/c-sharp/list-implementation-in-c-sharp/ below:

C# List Implementation - GeeksforGeeks

C# List Implementation

Last Updated : 11 Jul, 2025

In C#, a List is a generic collection used to store the elements or objects in the form of a list defined under System.Collection.Generic namespace. It provides the same functionality as ArrayList, the difference is a list is generic whereas ArrayList is a non-generic collection. It is dynamic means the size of the list grows, according to the need.

Example:

C#
// Creating and printing  a List
using System;
using System.Collections.Generic;

class Geeks
{
    public static void Main()
    {
        List<string> l = new List<string> { "C#", "Java", "Javascript" };
        
        foreach (string name in l)
        {
            Console.WriteLine(name);
        }
    }
}

Output
C#
Java
Javasccript
Creating List Using Constructors

The list class has 3 constructors which are used to create a list as follows:  

Example:

C#
// Creating List using Constructors
using System;
using System.Collections.Generic;

class Geeks
{
    public static void Main()
    {
        // default constructor creates an empty list
        List<int> list = new List<int>();
        list.Add(10);
        list.Add(20);
        Console.WriteLine("Default Constructor: ");
        foreach (var item in list)
        {
            Console.WriteLine(item);
        }

        // Construnctors from IEnumerable
        int[] num = { 10, 20 };
        List<int> enumerableList = new List<int>(num);
        Console.WriteLine("Constructor with IEnumerable: ");
        foreach (var item in enumerableList)
        {
            Console.WriteLine(item);
        }

        // Constructor with Initial Capacity 
        List<int> Clist = new List<int>(2);
        Clist.Add(10);
        Clist.Add(20);
        Console.WriteLine("Constructor with Initial Capacity: ");
        foreach (var item in Clist)
        {
            Console.WriteLine(item);
        }
    }
}

Output
Default Constructor: 
10
20
Constructor with IEnumerable: 
10
20
Constructor with Initial Capacity: 
10
20
Steps to Create a List

Step 1: Including System.Collection.Generics namespace.

using System.Collections.Generic;

Step 2: Create a list using the List<T> class.

List list_name = new List();

Performing Different Operations on List 1. Adding Elements

For adding elements list, The List<T> class provides two different methods which are: 

// Add element using Add method

list.Add(1);
list.Add(2);

// Adding elements using the 
// Collection initializers 
List<string> my_list1 = new List<string>() { “geeks”, “Geek123”, “GeeksforGeeks” }; 

2. Accessing List

We can access the elements of the list by using the following ways:

foreach loop: We can use a foreach loop to access the elements/objects of the List.

// Accessing elements of my_list 
// Using foreach loop 
foreach(int a in my_list) 

Console.WriteLine(a); 

ForEach loop: It is used to perform the specified action on each element of the List<T>.

// Accessing elements of my_list

// Using ForEach method

my_list.ForEach(a = > Console.WriteLine(a));

3. for loop: We can use a for loop to access the elements/objects of the List.

// Accessing elements of my_list 
// Using for loop 
for (int a = 0; a < my_list.Count; a++) 

Console.WriteLine(my_list[a]); 

Indexers: Indexers used to access the elements/objects of the List.

// Accessing elements of my_list 
// Using indexers 
Console.WriteLine(my_list[3]); 
Console.WriteLine(my_list[4]); 

Example:

C#
using System;
using System.Collections.Generic;

class Geeks
{
    public static void Main()
    {
        // Creating a list of integers
        List<int> my_list = new List<int> { 10, 20, 30,};

        // Accessing elements using foreach loop
        Console.WriteLine("Accessing elements using foreach loop:");
        foreach (int a in my_list)
        {
            Console.WriteLine(a);
        }

        // Accessing elements using ForEach method
        Console.WriteLine("Accessing elements using ForEach method:");
        my_list.ForEach(a => Console.WriteLine(a));

        // Accessing elements using for loop
        Console.WriteLine("Accessing elements using for loop:");
        for (int i = 0; i < my_list.Count; i++)
            Console.WriteLine(my_list[i]);
        

        // Accessing elements using indexers
        Console.WriteLine("Accessing elements using indexers:");
        Console.WriteLine($"Element at index 2: {my_list[2]}");
      
    }
}

Output
Accessing elements using foreach loop:
10
20
30
Accessing elements using ForEach method:
10
20
30
Accessing elements using for loop:
10
20
30
Accessing elements using indexers:
Element at index 2: 30
3. Remove Elements from the List

Example:

C#
// C# program to remove elements from the list
using System;
using System.Collections.Generic;

class Geeks
{
	static public void Main()
	{
		// Creating list using List class
		// and List<T>() Constructor
		List<int> l = new List<int>();

		// Adding elements to List
		// Using Add() method
		l.Add(1);
		l.Add(2);
		l.Add(3);
		l.Add(4);
		l.Add(5);

		// Initial count
		Console.WriteLine("Initial count:{0}", l.Count);
        
		l.Remove(3);
		Console.WriteLine("after removing 3");
		Console.WriteLine("2nd count:{0}", l.Count);

		l.RemoveAt(3);
		Console.WriteLine("after removing at 4th index");
		Console.WriteLine("3rd count:{0}", l.Count);

		l.RemoveRange(0, 2);
		Console.WriteLine("after removing range from 0 to 2");
		Console.WriteLine("4th count:{0}", l.Count);

		l.Clear();
		Console.WriteLine("after removing all elements");
		Console.WriteLine("5th count:{0}", l.Count);
	}
}

Output
Initial count:5
after removing 3
2nd count:4
after removing at 4th index
3rd count:3
after removing range from 0 to 2
4th count:1
after removing all elements
5th count:0
4. Sorting a List

We can sort the elements by using the Sort() method. Used to sort the elements or a portion of the elements in the List<T> using either the specified or default IComparer<T> implementation or a provided Comparison<T> delegate to compare list elements.

Example:

C#
// C# program to illustrate how
// sort a list
using System;
using System.Collections.Generic;

class Geeks {
    static public void Main()
    {
        // Creating list using List class
        // and List<T>() Constructor
        List<int> l = new List<int>();

        // Adding elements to List
        // Using Add() method
        l.Add(2);
        l.Add(1);
        l.Add(5);
        l.Add(3);
        l.Add(50);

        // Without sorted List
        Console.WriteLine("UnSorted List:");

        foreach(int a in l) Console.Write(a + ", ");
        Console.WriteLine();
     
        // using sort method
        l.Sort();

        Console.WriteLine("Sorted List:");
        foreach(int a in l) Console.Write(a + ", ");
    }
}

Output
UnSorted List:
2, 1, 5, 3, 50, 
Sorted List:
1, 2, 3, 5, 50, 
Ways to Implement List

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4