I've got a method that whose return type is a nullable int.
private int? LookupId(string name, string stateAbbreviation)
I'm trying to cleanup the code and decided to use a conditional operator on the return statement.
return id != 0 ? id : null;
Basically, if the id is not 0, go a head and return the id. It should never return 0 from the db. If, by chance, it is 0, return null.
The error is "Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and '' "
The conditional op is intended to replace a functioning If...Else statement.
Is there anything wrong with trying to use a conditional this way, in this combination? What am I missing?
Try return id != 0 ? Id : (int?)null;
?
You need something to coerce the type, like
return id != 0? (int?)Id : null
Is id
of type int?
(and not int
)?
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