Wednesday, November 8, 2017

Ternary expressions do not play nicely with nullable types in C#.

DateTime? x = String.IsNullOrWhiteSpace(y) ? null : Convert.ToDateTime(y);

 
 

...cannot compile and must become:

DateTime? x;
if (String.IsNullOrWhiteSpace(y))
{
   x = null;
}
else
{
   x = Convert.ToDateTime(y);
}

No comments:

Post a Comment