In .NET 6 we can set default return values for FirstOrDefault(), LastOrDefault() and SingleOrDefault() 

.NET 6 has a few LINQ improvements. One of them is support for custom default return values for FirstOrDefault(), LastOrDefault() and SingleOrDefault(). 

Normally when using these methods the built in default value for a type will be returned when the collection has no items, for example this would be null for reference types and 0 for int types. 

In .NET 6 we’ll be able to choose which default value gets returned when the collection is empty. Two examples are below…

Leave a Reply

Your email address will not be published.