Should I always return IEnumerable instead of IList?

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

    TL; DR; – summary

    • If you develop in-house software, do use the specific type(Like List) for the return values and the most generic type for input parameters even in case of collections.
    • If a method is a part of a redistributable library’s public API, use interfaces instead of concrete collection types to introduce both return values and input parameters.
    • If a method returns a read-only collection, show that by using IReadOnlyList or IReadOnlyCollection as the return value type.

    More

提交回复
热议问题