Should I always return IEnumerable instead of IList?

后端 未结 14 1989
一生所求
一生所求 2020-11-27 11:47

When I\'m writing my DAL or other code that returns a set of items, should I always make my return statement:

public IEnumerable GetRecentItems         


        
14条回答
  •  一生所求
    2020-11-27 12:45

    I think you can use either, but each has a use. Basically List is IEnumerable but you have count functionality, Add element, remove element

    IEnumerable is not efficient for counting elements, or getting a specific element in the collection.

    List is a collection which is ideally suited to finding specific elements, easy to add elements, or remove them.

    Generally I try to use List where possible as this gives me more flexibility.

    Use List getRecentItems() rather than IList GetRecentItems()

提交回复
热议问题