C# code won't compile. No implicit conversion between null and int
C#StringNullNullableC# Problem Overview
> Possible Duplicate:
> https://stackoverflow.com/questions/858080/nullable-types-and-the-ternary-operator-why-wont-this-work
Why doesn't this work? Seems like valid code.
string cert = ddCovCert.SelectedValue;
int? x = (string.IsNullOrEmpty(cert)) ? null: int.Parse(cert);
Display(x);
How should I code this? The method takes a Nullable
C# Solutions
Solution 1 - C#
int? x = string.IsNullOrEmpty(cert) ? (int?)null : int.Parse(cert);
Solution 2 - C#
I've come across the same thing ... I usually just cast the null to (int?)
int? x = (string.IsNullOrEmpty(cert)) ? (int?)null: int.Parse(cert);