I tend to only return IEnumerable<T>
when I want to hint to the caller that the implementation may use lazy evaluation. Otherwise, I'd usually return IList<T>
or ICollection<T>
, and implement as a ReadOnlyCollection<T>
if the result should be readonly.
Lazy evaluation can be an important consideration: if your implementation can throw an exception, this won't be thrown until the caller starts enumerating the result. By returning IList<T>
or ICollection<T>
, you're guaranteeing that any exception will be thrown at the point the method is called.
In the case of a WCF method, returning IEnumerable<T>
from a method that uses lazy evaluation means any exception might not be thrown until your response is being serialized - giving you less opportunity to handle it server-side.