4

Possible Duplicate:
Conditional operator assignment with Nullable<value> types?

Why does the conditional operator “?:” not work here when my function is returning a nullable integer “int?”? “return null” works but with “?:” I have to cast “null” to “(int?)” first.

    public int? IsLongName(string name) {

        int length = name.Length;

        // this works without problems
        if (name.Length > 10) {
            return null;
        } else {
            return name.Length;
        }

        // this reports: 
        // Type of conditional expression cannot be determined because 
        // there is no implicit conversion between '<null>' and 'int'
        return name.Length > 10 ? null : name.Length;
    }
Community
  • 1
  • 1
  • Don't we have a better duplicate? Or was it on eric's blog? I certainly remember a better answer to this question than the answer jon linked. – CodesInChaos Apr 21 '12 at 17:30
  • 1
    Check Eric Lippert's related blog entry: [Type inference woes, part one](http://blogs.msdn.com/ericlippert/archive/2006/05/24/type-inference-woes-part-one.aspx) – CodesInChaos Apr 21 '12 at 17:42

4 Answers4

5

Try changing your last line to this:

return name.Length > 10 ? null : (int?)name.Length;

The compiler can't understand what's the return type of the ?: operator. It has conflicting values - null and int. By casting the int to nullable, the compiler can understand the return type is nullable int, and null would be accepted as well.

SimpleVar
  • 14,044
  • 4
  • 38
  • 60
1

Both a null value and an int value can be implicitly converted to an int? data type, but a literal of null on its own isn't known by the compiler to be anything other than an object if you don't tell it. There is no common data type that both object and int can be implicitly converted to, which is what the compiler is complaining about.

As Yorye says, you can cast the int to int? to let the compiler do the conversion; or, you can cast the null to int?, which then allows the compiled to use the implicit conversion from int to int?.

David M
  • 71,481
  • 13
  • 158
  • 186
0

Both conditions of the ?: operator must be implicitly compatible. int can never be null, so there is a compile-time error (likewise, null can never be int). You have to cast, there's no way around it using the ternary.

I don't think you would have the same problem with an if statement, because the compiler would only check that the method returns a compatible value to the return type from any given path, not that the return value from any given exit point is implicitly compatible with another block's return value.

Joseph Yaduvanshi
  • 20,241
  • 5
  • 61
  • 69
0

The ?: operator only considers the types of its two possible return values. It is not aware of the type of the variable that will receive its result (indeed, in more complex expressions, no explicit variable may exist).

If one of those return values is null, it has no type information - it can only check the other return value's type, and check whether a conversion exists.

In your case, we have null and a return value of type int. There is no conversion available. There would be a conversion to int?, but that is neither of the possible return types that ?: is considering.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448