Google PlusFacebookTwitter


By on Sep 2, 2016 in Development | 0 comments

David Share On GoogleShare On FacebookShare On Twitter

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.

Submit a Comment

E-postadressen publiceras inte. Obligatoriska fält är märkta *