Should I always return IEnumerable instead of IList?

后端 未结 14 1938
一生所求
一生所求 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:37

    List<T> offers the calling code many more features, such as modifying the returned object and access by index. So the question boils down to: in your application's specific use case, do you WANT to support such uses (presumably by returning a freshly constructed collection!), for the caller's convenience -- or do you want speed for the simple case when all the caller needs is to loop through the collection and you can safely return a reference to a real underlying collection without fearing this will get it erroneously changed, etc?

    Only you can answer this question, and only by understanding well what your callers will want to do with the return value, and how important performance is here (how big are the collections you would be copying, how likely is this to be a bottleneck, etc).

    0 讨论(0)
  • 2020-11-27 12:39

    If you do not counting in your external code it is always better to return IEnumerable, because later you can change your implementation (without external code impact), for example, for yield iterator logic and conserve memory resources (very good language feature by the way).

    However if you need items count, don't forget that there is another layer between IEnumerable and IList - ICollection.

    0 讨论(0)
  • 2020-11-27 12:40

    That depends...

    Returning the least derived type (IEnumerable) will leave you the most leeway to change the underlying implementation down the track.

    Returning a more derived type (IList) provides the users of your API with more operations on the result.

    I would always suggest returning the least derived type that has all the operations your users are going to need... so basically, you first have to deremine what operations on the result make sense in the context of the API you are defining.

    0 讨论(0)
  • 2020-11-27 12:43

    It's not so simple when you are talking about return values instead of input parameters. When it's an input parameter, you know exactly what you need to do. So, if you need to be able to iterate over the collection, you take an IEnumberable whereas if you need to add or remove, you take an IList.

    In the case of a return value, it's tougher. What does your caller expect? If you return an IEnumerable, then he will not know a priori that he can make an IList out of it. But, if you return an IList, he will know that he can iterate over it. So, you have to take into account what your caller is going to do with the data. The functionality that your caller needs/expects is what should govern when making the decision on what to return.

    0 讨论(0)
  • 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<FooBar> getRecentItems() rather than IList<FooBar> GetRecentItems()

    0 讨论(0)
  • 2020-11-27 12:46

    as all have said it depends, if you don't want Add/Remove functioanlity at calling layer then i will vote for IEnumerable as it provides only iteration and basic functionality which in design prespective i like. Returning IList my votes are always againist it but it's mainly what you like and what not. in performance terms i think they are more of same.

    0 讨论(0)
提交回复
热议问题