This is something I've come across while refactoring some legacy code.
Consider a method on an interface that returns a Task
:
public interface IFoo
{
Task Bar();
}
The Bar
method implementation can be implemented in two ways:
Returning a Task
:
public class Foo1 : IFoo
{
public Task Bar()
{
return Task.Run(() =>
{
/* some work */
});
}
}
Or using async
... await
:
public class Foo2 : IFoo
{
public async Task Bar()
{
await Task.Run(() =>
{
/* some work */
});
}
}
Are these implementations functionally equivalent, or are there (potentially subtle) differences?