Is there any difference between those two ways to write the same thing?
int? foo = GetValueOrDefault();
var obj = new
{
//some code...,
bar = foo.HasValue ? foo > 0 : (bool?)null
}
VS
int? foo = GetValueOrDefault();
var obj = new
{
//some code...,
bar = foo.HasValue ? foo > 0 : default(bool?)
}
It is the same. A
Nullable<bool>is a struct and the language specification states:Since a
Nullable<T>has these two fields:So yes, using
default(bool?)has the same effect as using(bool?)null, because(bool?)nullis also aNullable<bool>withhasValue=false(same as usingnew Nullable<bool>()).Why you can assign
nullat all to aNullable<T>which is a struct, so a value type? Well, that is compiler magic which is not visible in the source.