List, IList, IEnumerable, IQueryable, ICollection, which is most flexible return type?

前端 未结 3 636
感动是毒
感动是毒 2020-11-30 21:34

I\'ve seen this question posted here previously but I\'m not satisfied that I understand the complete ramifications. The problem is what return type should a data layer that

3条回答
  •  被撕碎了的回忆
    2020-11-30 21:59

    You should always return an interface rather than a concrete type, this goes without saying as it specifies the behaviour allowed without tying the consumer to a specific implementation.

    In terms of which interface to return, you should think about the purpose of the method and the intentions of the caller. If you return a collection, should the caller be able to alter the collection? e.g. add/remove items? if all they need to do is enumerate it (do a foreach) then you should just return an IEnumerable.

提交回复
热议问题