Search code examples
c#interfaceilist

Why to use interface IList to create an object of List type?


Why here interface Ilist is used to create an object of List type

 IList<BankAccountView> bankAccountViews = new List<BankAccountView>();

when it can be done like this

 List<BankAccountView> bankAccountViews = new List<BankAccountView>();

Solution

  • A List is a concrete type, while an IList is a contract for which you can use any implemtation that an IList has.

    An IList has a set of methods as defined on MSDN: http://msdn.microsoft.com/en-us/library/system.collections.ilist.aspx

    Here's a very good blogpost explaining it in detail: http://www.claudiobernasconi.ch/2013/07/22/when-to-use-ienumerable-icollection-ilist-and-list/

    enter image description here