Search code examples
listilist

Which one to use between IList<T> and List<T>


I have list of emplyoees, then which one I should use between IList and List and why? I am using C#.

In which scenario I use List and IList.

Thank you.


Solution

  • IList represent a non-generic collection for collection of objects. If you use IList you can create customized Lists and use advantages of DIP (Dependency Inversion Principle) of OOP.

    SimpleList<T> : IList<T>
    {
        ...
    } 
    
    IList<T> list1 = new List<T>();
    IList<T> list2 = new SimpleList<T>();
    

    You behave same with these two kind of lists.