Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is default(TSource) a real method?

Tags:

c#

.net

vb.net

linq

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.

like image 429
miroxlav Avatar asked Feb 12 '23 08:02

miroxlav


1 Answers

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.

like image 144
Codor Avatar answered Feb 15 '23 09:02

Codor