I am trying to have multiple non-async methods run at the same time as below. All functions are defined in the same class as member functions.
private string getEstimatedWaypointStart()
{
return "id-1";
}
private string getEstimatedWaypointEnd()
{
return "id-2";
}
private void postVector()
{
//These don't compile
Task t1 = Task.Run(getEstimatedWaypointStart);
Task t2 = Task.Run(getEstimatedWaypointEnd);
//These works fine
Task t3 = Task.Run((Func<string>)getEstimatedWaypointStart);
Task t4 = Task.Run(() => getEstimatedWaypointEnd());
/*do stuff*/
}
However, I’m getting unexpected error complaining about ambiguousness when I try to build the code:
CS0121
The call is ambiguous between the following methods or properties: 'Task.Run<TResult>(Func<TResult>)' and 'Task.Run(Func<Task>)'
I’ve found this post having a really similar situation. However, in that post the delegate was not ambiguous after removing the unnecessary invoke
since its type is defined in the method’s parameter. I believe this is the same reason why a cast (Func<string>
) fixes the problem for me. Not in my case though as the functions are just defined in the same class.
I’m not looking for a workaround since a lambda expression works fine. I am curious if there is an explanation of this behavior: Why the compiler is seeing the delegate as a Func<Task>
when the async
keyword is not used at all? Is this a bug in my current fairly old C# version (C# 7.3) or is this expected? If later, should I just stick with the lambda expression?