You are probably referring to the following leading to a compiler error:
int? nullableInt = (somecondition) ? value : null;
Indeed, you need to add a cast here:
int? nullableInt = (somecondition) ? value : (int?)null;
Although I cannot explain this in detail, I suspect the following:
int? actually a short form for a Nullable<int> , so basically it is an instance of an object. When set to int nullableInt Nullable<int> property will be set internally. Direct null assignment will also be fine.
However, a conditional assignment will return two different types: int if somecondition is true and object ( null ) otherwise.
Now the compiler does not know how to handle this, since the ternary operator must return values of the same type. Therefore, you need to specify the desired type for the null value.
Sorry if this is not a very deep technical explanation - I am sure there is someone who can better understand this, but it can help to understand it better.
source share