locked
List vs Ilist RRS feed

  • Question

  • User-922170241 posted

    hi to all, thanks in advance .

    what is  the exact difference b/w list and iList with real time example ?

    Monday, December 15, 2014 8:51 AM

Answers

  • User-760709272 posted

    Use List<T> when you want to use an actual List<T> collection.  Use IList<T> when you don't really care what concrete class is used, the class just has to implement IList<T>.

    static void ProcessCollectionA(List<int> data)
    {
        foreach (int i in data)
        {
            Console.WriteLine(i);
        }
    }
    
    static void ProcessCollectionB(IList<int> data)
    {
        foreach (int i in data)
        {
            Console.WriteLine(i);
        }
    }
        
    
    static void Main(string[] args)
    {
        List<int> dataA = new List<int>{1,2,3,4,5};
        ReadOnlyCollection<int> dataB = new ReadOnlyCollection<int>(new int[] { 1, 2, 3, 4, 5 });
    
        // ProcessCollectionA accepts List<int> so you must pass that class
        ProcessCollectionA(dataA);
    
        // The below won't compile as dataB is not List<int>
        //ProcessCollectionA(dataB);
    
        // Both of these work as ProcessCollectionB doesn't care about the concrete type you pass
        // it only cares that the type implements IList<int>
        ProcessCollectionA(dataA);
        ProcessCollectionB(dataB);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 15, 2014 9:09 AM

All replies

  • User-821857111 posted

    IList is an interface. List is a concrete implementation of the IList interface. You cannot instantiate an interface.

    To illustrate:

    IList<string> myStringList = new IList<string>(); // will not compile - Cannot create an instance of the abstract class or interface
    IList<string> myStringList = new List<string>(); // will compile as the type being instantiated is List, which implements IList



    Monday, December 15, 2014 8:54 AM
  • User-760709272 posted

    Use List<T> when you want to use an actual List<T> collection.  Use IList<T> when you don't really care what concrete class is used, the class just has to implement IList<T>.

    static void ProcessCollectionA(List<int> data)
    {
        foreach (int i in data)
        {
            Console.WriteLine(i);
        }
    }
    
    static void ProcessCollectionB(IList<int> data)
    {
        foreach (int i in data)
        {
            Console.WriteLine(i);
        }
    }
        
    
    static void Main(string[] args)
    {
        List<int> dataA = new List<int>{1,2,3,4,5};
        ReadOnlyCollection<int> dataB = new ReadOnlyCollection<int>(new int[] { 1, 2, 3, 4, 5 });
    
        // ProcessCollectionA accepts List<int> so you must pass that class
        ProcessCollectionA(dataA);
    
        // The below won't compile as dataB is not List<int>
        //ProcessCollectionA(dataB);
    
        // Both of these work as ProcessCollectionB doesn't care about the concrete type you pass
        // it only cares that the type implements IList<int>
        ProcessCollectionA(dataA);
        ProcessCollectionB(dataB);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 15, 2014 9:09 AM