You must Sign In to post a response.
  • Category: ASP.Net MVC

    When to take IEnumerable and when to take List in model of MVC?

    Hi,
    When to take IEnumerable and when to take List in model of MVC?
    Please help.

    Best regards,
    Prachi Kulkarni.
  • #709705
    List:
    List can be indexed by integral index, also can add or remove elements to the list and can insert an item at a particular index. List can do by integral index.

    IEnumerable:
    IEnumerable faster than List. It is present in System.Collections Namespace.While query data from database, IEnumerable execute select query on server side, load data in-memory on client side and then filter data.It not suitable for paging becuase it doesn't support lazy loading. IEnumerable<T> is just a sequence of items. FOR-EACH loop is the only way to iterate through IEnumerable collection. we can't access the items randomly

    Regards,
    T.N. THEAAVARAAJ
    Senior Software Engineer,
    Microsoft Certified Technology Specialist.
    Email:devabe2005@gmail.com

  • #709709
    IEnumerable gives a forward only collection which can be iterate using for each loop.
    if you want only to read all items in the collection you can go for IEnumerable.
    you cannot modify any of the item while in the loop.
    also the count of the collection can be found only after the complete iteration.

    IList give you a collection which can be iterated over using the index.
    so it gives the flexibility to get the items with given indexes.
    you need not go through the entire collection.
    also you can modify members in the collection.
    you will get the collection items count before hand.

    so depending on what is your need,you can select either IEnumerable or IList

    Regards,
    Shine


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.