Extending LINQ to accept nullable enumerables

What penalties are associated with my extension method at runtime?

Your extension method is transformed into a state-machine, so there’s the minimal overhead of that, but that shouldn’t be noticeable.

Is it’s a good practice to extend linq that way?

In your question you state:

While working with Linq extensions it’s normal to see code like this (insert enumerable null check here)

And I beg to differ. The common practice says don’t return null where an IEnumerable<T> is expected. Most cases should return an empty collection (or IEnumerable), leaving null to the exceptional, because null is not empty. This would make your method entirely redundant. Use Enumerable.Empty<T> where needed.

Leave a Comment