According to Visual Studio this is not ok:
var foo = null;
But this is ok:
var foo = false ? (double?)null : null;
<
Implicitly typed variable declaration/assignment serves two purposes:
Your first declaration has null
for the value, with no way to figure out the type (it could be anything derived from System.Object
, or a Nullable
). That is why it is an error.
Your second declaration pinpoints the type as Nullable
because of the cast. That is why C# allows it.
It goes without saying that double? foo = null
would be much easier to read.