Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning null/Nullable to DateTime in Ternary Operation

Tags:

I have a statement like

DateTime ? dt = (string1 == string2) ? null; (DateTime)(txtbox.Text); 

which I cannot compile. Reason is : null cannot be assigned to DateTime.

So, I have to declare a Nullable<DateTime> nullable variable and replace null with nullable.

I do not want to use if-statement and I want to do this in one line.

Also, Can I use operator ?? here.

like image 733
iTSrAVIE Avatar asked Jun 02 '11 13:06

iTSrAVIE


1 Answers

DateTime? dt = (string1 == string2) ? (DateTime?)null                                     : DateTime.Parse(txtbox.Text); 
like image 124
LukeH Avatar answered Oct 02 '22 00:10

LukeH