default(Nullable(type)) vs default(type)

Eugene picture Eugene · Oct 12, 2011 · Viewed 14.2k times · Source

In C#, is there a difference between default(Nullable<long>) (or default(long?)) and default(long) ?

Long is just an example, it can be any other struct type.

Answer

Jeff Mercado picture Jeff Mercado · Oct 12, 2011

Well yes. The default value of a nullable or other reference type is null while the default value for a long or other value type is 0 (and any other members set to their defaults).

In this case:

default(Nullable<long>) == null
default(long?) == null

default(long) == 0L