After defining a variable of a nullable type, when trying to assign it through a conditional operator, if I try to assign
By design. Conditional operator can figure out correct return type only in case if one side of expression can be implicitly converted to another.
int and null type can not be converted in any direction.
To demonstrate this - you must be aware that there are 2 workarounds - not one.
yours
int? a = true ? (int?) null : 12;
as well
int? a = true ? null : (int?)12;
This will make following pairs of types - int? and int and null type and int?.
Something I agree - since null-type is special case and very used often - this will be nice to make it special in this situation also.
No comments:
Post a Comment