Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

12
  • I think this answer most directly addresses the heart of the question, especially the first sentence. Spot on. Commented Jun 21 at 0:48
  • 5
    I agree that array or List is (usually) too strong a contract. In my personal experience, though, IEnumerable is often too weak: It does not even guarantee that the collection is materialized, which is something (a) that I am often willing to guarantee and (b) that avoids extra defensive .ToList() calls at the consumer that would be needed if the collection were to be enumerated more than once. Hence, ICollection<T> has become my go-to type for returning collections in business software, as a compromise between the two extremes. Commented Jun 23 at 8:16
  • @Heinzi Materializing a large collection is something I often have to avoid. In Java this is done with Stream which does not support multiple traversals. The argument about traversals does not hold for C# though, given the Reset method. Commented Jun 23 at 11:06
  • 5
    @coredump this approach produces a brittle API. Do not expose any information unless there is a specific need for it. Commented Jun 23 at 11:32
  • 1
    Maybe this is different in C#, but if I return a more precise type when implementing an interface, users that manipulate the data through the interface only see the general type. The internal layer sees the actual type. That's probably the thing I did not communicate well here. This is like having two methods, the internal one that returns an array and the public one that wraps it and returns the IEnumerable. Commented Jun 23 at 16:16