Should I always return IEnumerable instead of IList?

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

    I think the general rule is to use the more specific class to return, to avoid doing unneeded work and give your caller more options.

    That said, I think it's more important to consider the code in front of you which you are writing than the code the next guy will write (within reason.) This is because you can make assumptions about the code that already exists.

    Remember that moving UP to a collection from IEnumerable in an interface will work, moving down to IEnumerable from a collection will break existing code.

    If these opinions all seem conflicted, it's because the decision is subjective.

提交回复
热议问题