Returning null as an int permitted with ternary operator but not if statement

Let’s look at the simple Java code in the following snippet: public class Main { private int temp() { return true ? null : 0; // No compiler error – the compiler allows a return value of null // in a method signature that returns an int. } private int same() { if (true) { … Read more

Nullable types and the ternary operator: why is `? 10 : null` forbidden? [duplicate]

This question already has answers here: Conditional operator assignment with Nullable<value> types? (6 answers) Why doesn’t this C# code compile? (4 answers) Closed 9 years ago. I just came across a weird error: private bool GetBoolValue() { //Do some logic and return true or false } Then, in another method, something like this: int? x … Read more