Return value and signature abiguity when assigning to delegates -- Func<Task> vs Action

79 Views Asked by At

I was surprised to find out that the return type on a method can create an overload ambiguity when passing the method in to another method. Since the return type is not part of the signature, it's hard to see how changing the return value could create ambiguity where it had not previously existed. Yet this seems to be the case for void and Task. Consider the following:

    class Signature
    {
        static public void Overload(Func<Task> countasync)
        {
        }
        static public void Overload(Action count)
        {
        }
    }

    void Decrement() { }
    Task IncrementAsync() { return Task.CompletedTask; }

    void TestSig()
    {
        Signature.Overload(this.IncrementAsync); // no compile time error
        Signature.Overload(this.Decrement); // compile time error: this call is ambiguous 
    }

Is there a way to define the Overload argument types so that the second call is not ambiguous, while still allowing the first call?

1

There are 1 best solutions below

1
Kyle On BEST ANSWER

No, there's no way to fix the overload resolution.

But, you can remove the ambiguity at the cost of slightly uglier call site code:

Signature.Overload( () => this.Decrement() );

The compiler will correctly deduce the overload in that case.