Wednesday, April 15, 2015

.FirstOrDefault came up in a conversation last night.

In C# it will behave a lot like .Single but it will not throw an exception if there is not just one item returned from filtering a collection. This should allow you to be more evil, clop around all cloven-hooved.

 
 

Addendum 12/19/2015: .FirstOrDefault will not break when it finds nothing. It will return null. In the case of value types, such as a list of int, it will return a default value (which would be 0 for int types).

No comments:

Post a Comment