top of page
Writer's pictureThe Tech Platform

IList<T> and List<T> Performance in C#.Net

List<T>

List is a class that implements various interfaces. The programmer can create an object of List<T> object and assign it to any of its interface type variables. Moreover, it is possible to create an object of List as follows.

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

Example:

using System.IO;
using System;
using System.collections.Generic;

class Program
{
    static void Main()
    {
        List<int> obj = new List<int>();
        obj.Add(1);
        obj.Add(2);
        obj.Add(3);
        obj.Add(4);
        
        obj.RemoveAt(2);
        
        foreach (var num in obj)
            Console.Write(num + "");
    }
}


Output:









IList<T>

List<T> is a concrete implementation of IList<T> interface. In OOP, it is a good programming practice to use interfaces than using concrete classes. Therefore, the programmer can use IList>T> type variable to create an object of List<T>.


It is possible to create a IList object as follows.

IList<int> intList = new List>int>();

Example:

using System.IO;
using System;
using System.collections.Generic;

class Program
{
    static void Main()
    {
        List<int> obj = new List<int>();
        obj.Add(1);
        obj.Add(2);
        obj.Add(3);
        obj.Add(4);
        
        obj.RemoveAt(2);
        
        foreach (var num in obj)
            Console.Write(num + "");
    }
}

Output:








Difference Between List<T> and IList<T>

List<T>

IList<T>

List<T> is class that represents the list of objects which can be accessed by the index

IList<T> is an interface that represents a collection of objects that can be individually accessed by the Index.

It has more number of helper methods

It has less number of helper methods

It is a class

It is an interface


When to use List and when use IList in C#

  • A List object allows you to create a list, add things to it, remove it, update it, index into it and etc. List is used whenever you just want a generic List where you specify object type in it and that's it.

  • IList on the other hand is an Interface. Basically, if you want to create your own type of List, say a list class called ProductList, then you can use the Interface to give you basic methods and structure to your new class. IList is for when you want to create your own, special sub-class that implements List.

  • IList is an Interface and cannot be instantiated. List is a class and can be instantiated. It means:

          IList<string> MyList = new IList<string>();
          List<string> MyList = new List<string>



The Tech Platform

0 comments

Comments


bottom of page