Recently, my attitude has changed a bit toward IEnumerable<>.

{{i}}{{a}}



{{/a}}{{/i}}

Recently, my attitude has changed a bit toward IEnumerable<>. It has always served a very good purpose (foreach is so nice), but it has seemed to me to be an interesting, but mostly hidden implementation detail of the framework. And then there was LINQ.

LINQ is a ton of functionality built around that simple little interface. Which makes me think about using it, exposing it directly in my own API. Why would I choose to expose IEnumerable<>, instead of IList<> or something else? First of all, what other interface is there for exposing a read-only set of "things"? Second, I adhere to the philosophy APIs should expose only the necessary functionality and no more, because:

  1. It makes it easier to understand how to use the API (and how not to use it).
  2. It makes it easier to change or completely re-implement the original API.
  3. The less you can do, the less there is to go wrong.

Of course, IEnumerable<> may be useful, but it certainly isn't pretty. Instead of IEnumerable<Foo>, I would rather see Foo* or Foo+ orwell, you get the idea.

This is not a new idea (I'm sorry, but you probably won't find those on this blog). There is, of course, Foo?, which means Nullable<Foo>. And then there is the experimental language C (thank you, cut-and-paste), which proves how unoriginal this idea is: In C, Foo* is a "stream" of Foo. According to the C Overview, "Streams in C are closely related to IEnumerable<>". Well, who's going to argue with the brains inside MS Research? Not me!

Posted in Programming Post Date 01/05/2025


Comments

Name


Email


Website


Comment


Recent Posts