3

I just stumbled on the following behavior, which I don't see the motivation for.

Consider the following methods:

void Foo(Action bar)
{
     bar();
     Console.WriteLine("action");
}

async Task Foo(Func<Task> bar)
{
    await bar();
    Console.WriteLine("func");
}

void Bar()
{
}

This definition causes a compiler error, stating that the call to Foo is ambiguous between the two methods:

void Baz()
{
    Foo(Bar); // <-- compiler error here
}

However, this works fine:

void Baz()
{
    Foo(() => Bar());
}

Why is this even ambiguous (Bar() doesn't return a Task, so Bar shouldn't be a Func<Task>)? Why does it work when I use a lambda expression instead of a method group?

Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402

0 Answers0