DEV Community

Ajeeth thangarasu
Ajeeth thangarasu

Posted on

How to create C# List<T> generics

C# List<T> generics

  • C# List<T> is a strongly typed collection for creating a list of objects of type <T>. 
  • It is similar to ArrayList, we can add, remove, search, sort, manipulate list-objects.
  • In C#, ArrayList is a non-generic collection whereas List<T> is a generic collection.
  • List<T> has high performance than ArrayList and type-safe. 
  • List<T> has similar methods and properties as the ArrayList.

Creating a C# List<T>

  • List<T> is created by using a new operator either it can be empty capacity or specify the size.

Syntax:

List<T> <list_name> = new List<T>(<capacity>);

  • <T> is the type of object to be created like int, float, double etc.
  • <list_name> name of the list.
  • <capacity> is the size of the list which is optional.

Eg:

List<string> List1 = new List<string>();

Adding object to List<T>

  • Add(T obj) method will add the element type <T> in the list on by one.

Eg:

List1.Add(“Element1”);

  • Insert(int index, T obj) method will add the element at the specified index.

Eg:

List1.Insert(1,”Element1”);

Removing object to List<T>

  • Remove(T obj) method will remove the element in the list.

Eg:

List1.Remove(“Element1”);

  • RemoveAt(int index) Removes the element at the specified index.

Eg:

List1.RemoveAt(1);

Get the size of List<T>:

  • Count property is used to get the size of the list.

Eg:

List1.Count();

To sort the List<T>:

  • Use the Sort() method to sort the array in ascending order.

Eg:

List1.Sort();

Accessing the elements in the List<T>

  • The index number can be used to access the elements like an array.

Eg:

List1[0] // accessing element at index 0;

  • To iterate over the List<T>, use foreach function.

Eg:

foreach(string str in List1)

{

Console.WriteLine(str);

}

Example:

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        //create new List<T> of string type using new operator.
        List<string> list1=new List<string>();

        //Adding elements using Add(T) method.
        list1.Add("Element1");
        list1.Add("Element2");
        list1.Add("Element3");
        list1.Add("Element4");
        list1.Add("Element5");


        Console.WriteLine("Printing elements in list1");
        Console.WriteLine("Size of list1: "+list1.Count);//prinitng size of list1 using count

        //printing list1 elements using foreach
        foreach(string str in list1)
        {
            Console.WriteLine(str);
        }

        //removing 'Element1' 'Element2' using Remove(T) method.
        list1.Remove("Element1");
        list1.Remove("Element2");

        Console.WriteLine();

        Console.WriteLine("Printing elements in list1 after removing Element1, Element2");
        Console.WriteLine("Size of list1: "+list1.Count);//prinitng size of list1 using count

        //printing list1 elements using foreach
        foreach(string str in list1)
        {
            Console.WriteLine(str);
        }

        list1.Insert(2,"Element1");//inserting 'Element1' in index 2
        list1.Insert(4,"Element2");//inserting 'Element2' in index 4

        Console.WriteLine();

        Console.WriteLine("Printing elements in list1 after inserting Element1, Element2");
        Console.WriteLine("Size of list1: "+list1.Count);//prinitng size of list1 using count
        //printing list1 elements using foreach
        foreach(string str in list1)
        {
            Console.WriteLine(str);
        }

        list1.Sort();//sorting list1

        Console.WriteLine();

        Console.WriteLine("Printing elements in list1 after sorting");
        //printing list1 elements using foreach
        foreach(string str in list1)
        {
            Console.WriteLine(str);
        }

    }
}

Enter fullscreen mode Exit fullscreen mode

Output:

Printing elements in list1
Size of list1: 5
Element1
Element2
Element3
Element4
Element5

Printing elements in list1 after removing Element1, Element2
Size of list1: 3
Element3
Element4
Element5

Printing elements in list1 after inserting Element1, Element2
Size of list1: 5
Element3
Element4
Element1
Element5
Element2

Printing elements in list1 after sorting
Element1
Element2
Element3
Element4
Element5

It is recommended to use List<T> generic over ArrayList since it is type-safe, high performance. 

To more refer:https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-5.0#remarks


For my other programming archives -> Go.

If any suggestions please comment about it.

Top comments (0)