I have two overloads of the method Foo
, one for synchronous and one for asynchronous delegates:
public static void Foo<T>(Func<T> function, List<T> list = null) { }
public static void Foo<T>(Func<Task<T>> function, List<T> list = null) { }
This method accepts a function
, and a second optional argument of type List<T>
.
When I try to call this method without specifying the type <T>
, the compiler is smart enough to resolve the ambiguity:
Foo(async () => { await Task.CompletedTask; return 0;}); // OK
...unless I attempt to pass explicitly null
as the second argument:
Foo(async () => { await Task.CompletedTask; return 0;}, null); // Error CS0121
In that case I get the following compilation error:
The call is ambiguous between the following methods or properties:
'Test.Foo<T>(Func<T>, List<T>)'
and'Test.Foo<T>(Func<Task<T>>, List<T>)'
The strange thing is that null
is the default value of this argument anyway!
Is this a limitation of the compiler that could be fixed, or an unsolvable case of ambiguity that can't be handled in any other way than by a compilation error?
C# 8, Visual Studio 16.3.9, .NET Framework 4.8, .NET Core 3.0
Update: Here is a helpful answer to this question.