Now I use the enumerable extensions in C# a lot. First(), Single(), Select() is everywhere. However, I’ve found that there’s a few I’m missing and that sometimes they just don’t exactly suit my purposes. So, sometimes I create an additional extension method or two, like the two below:

TryGetFirst I use because the Try-pattern is nicer sometimes than the if(result != null) pattern.

The TryGetSingle is mostly due to the fact that the Single() enumerable extension throws (or at least used to throw) the same message for when there were no items in the list and when there were more than 1. I usually find that if there’s more then one, then there’s an error and if there isn’t one I want to handle that in a different way.

Why not join the newsletter?

Why not join the newsletter?

If you're not a fan of facebook or twitter, sign up to the newsletter and I'll send you a quick update every so often with what's going on and new posts coming out.

You have Successfully Subscribed!