Similar to Implementing an interface that requires a Task return type in synchronous code although I\'m curious if I should just ignore the compiler error my situation gener
As everybody else already pointed out, you have 3 different options, so it's a matter of opinion:
async
and ignore the warning async
overloads async
and return a completed task.I would recommend returning an already completed task:
public class SimplyAwesome : IAmAwesome
{
public Task MakeAwesomeAsync()
{
// run synchronously
return TaskExtensions.CompletedTask;
}
}
For several reasons:
async
has a slight overhead of creating a state-machine and disabling some optimizations (like inlining) because of the try-catch block the compiler adds.await
is.async
. It's like adding while(false){}
in your code, it will act the same, but it doesn't convey the meaning of the method.Servy pointed out that returning a task changes the semantics of exception handling. While that's true, I think it's a non-issue.
First of all, most async
code calls a method and awaits the returned task at the same place (i.e. await MakeAwesomeAsync()
) which means the exception would be thrown at the same place no matter whether the method was async
or not.
Second of all, even the .Net framework's Task-returning methods throw exceptions synchronously. Take for example Task.Delay
which throws an exception directly without storing it in the returned task, so there's no need to await
the task to raise the exception:
try
{
Task.Delay(-2);
}
catch (Exception e)
{
Console.WriteLine(e);
}
Since .Net developers need to except to encounter exceptions synchronously in .Net it's reasonable they should also except that from your code.