Search code examples
c#.netpolymorphismasp.net-web-api2conditional-operator

"Cannot be determined because there is no implicit conversion" with ternery if return


I have the following ASP.NET Web Api 2 action with a ternary if return:

[HttpDelete]
public IHttpActionResult Delete()
{
    bool deleted;

    // ...

    return deleted ? this.Ok() : this.NotFound();
}

I receive a

Type of conditional expression cannot be determined because there is no implicit conversion between 'System.Web.Http.Results.OkResult' and 'System.Web.Http.Results.NotFoundResult'

when they both implement IHttpActionResult.

However if I remove the ternary if, the compiler is happy:

if (deleted)
{
    return this.Ok();
}
return this.NotFound();

Why is this?


Solution

  • You need to explicitly cast the result to IHttpActionResult:

    return deleted ? (IHttpActionResult) this.Ok() : this.NotFound();
    

    Edit:

    As for Grants question:

    Why does Sam's second block of code work without explicitly casting to IHttpActionResult, just out of curiosity? Is this something particular to the conditional ?: operator?

    Lets create a simple demonstration. Assume the following code:

    public interface IFoo { }
    
    public class B : IFoo { }
    
    public class C : IFoo { }
    

    And then the following:

    public class A
    {
        IFoo F(bool b)
        {
            return b ? (IFoo) new B() : new C();
        }
    }
    

    Lets see how the compiler de-compiles the ternary operator:

    private IFoo F(bool b)
    {
        IFoo arg_13_0;
        if (!b)
        {
            IFoo foo = new C();
            arg_13_0 = foo;
        }
        else
        {
            arg_13_0 = new B();
        }
        return arg_13_0;
    }
    

    The explicit cast is enough for the compiler to infer that the variable should be of type IFoo and hence satisfy our whole if-else. That is why it is enough for us to "hint" the compiler only once of our type cast.

    @dcastro has referenced the exact part of the language specification which determines the control of the type, see that for text-book definition.