How can I write
string date = "12/2/2011";
DateTime? dt = date ?? DateTime.Parse(date);
this throws a compile time error. I know I can do tryparse or do if {}. Is there any way to do this in one line?
Try using the conditional operator ?: instead of the null-coalescing operator ??:
DateTime? dt = date == null ? (DateTime?)null : DateTime.Parse(date);
You also need to cast the null to DateTime? otherwise you will get a compile error.
string date = "12/2/2011";
DateTime? dt = String.IsNullOrEmpty(date) ? (DateTime?)null : DateTime.Parse(date);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With