In MSDN help page on Enumerable.FirstOrDefault Method there is a method result explained as:
default(TSource) if source is empty; otherwise, the first element in source.
Remarks section contains note:
The default value for reference and nullable types is null.
I was always doing check for null
(VB.NET: Nothing
) value but is there some default(TSource)
which can be used instead of null/Nothing
literal? (For example default(int)
.)
I cannot find default(TSource)
method, but it is mentioned on help page. Or isn't it a method?
EDIT: default(TSource)
is visible on MSDN page for both C# and VB and I'm interested in answer covering both languages.
You can use the default
operator, which is documented here. It returns null
for reference types, and a type-dependent default value for value types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With