mercredi 23 décembre 2015

Ternary operator (?) not working

Why doesn't this work?

DateTime? date = condition?DateTime.Now: null; //Error: no implicit conversion between DateTime and null

While this does?

DateTime? date;
if (condition)
{
  date = DateTime.Now;
}
else
  date = null;

A somewhat similar problem can be found here, but I just cannot make the association. Thanks for your help..

Aucun commentaire:

Enregistrer un commentaire