Difference Between List And IList In C#

In this article,I have described difference between List and IList in C#

  • List is a class that represents a list of objects which can be accessed by index.
  • IList is an interface that represents a collection of objects which can be accessed by index.
  • List<> is more number of helper methods.
  • IList<> is more number of helper methods.
  • Below I have create an object of List<T> object and assign it to any of its interface type variables.

Example of  List :

using System;
using System.Collections.Generic;

namespace ListVsIList
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> newlist = new List<string>();
            newlist.Add("A");
            newlist.Add("B");
            newlist.Add("C");
            newlist.Add("D");
                           
            foreach (var item in newlist)
            {
                Console.WriteLine(item + " ");
            }
            Console.ReadLine();
        }
    }
}

Output :

Example of IList :

using System;
using System.Collections.Generic;

namespace ListVsIList
{
    class Program
    {
        static void Main(string[] args)
        {
            IList<int> intlist = new List<int>();
            intlist.Add(1);
            intlist.Add(2);
            intlist.Add(3);
            intlist.Add(4);
            intlist.Add(5);
                 
            foreach (var num in intlist)
            {
                Console.WriteLine(num + " ");
            }
            Console.ReadLine();
        }
    }
}

Output :

Submit a Comment

Your email address will not be published.

Subscribe

Select Categories