What's the practical difference (if any) between these two methods?
public static Task DoSomething()
{
return FooAsync();
}
public static async Task DoSomethingAsync()
{
await FooAsync();
}
What's the practical difference (if any) between these two methods?
public static Task DoSomething()
{
return FooAsync();
}
public static async Task DoSomethingAsync()
{
await FooAsync();
}
The practical difference in terms of using these is: nothing. Both are 'awaitable'.
There will be a small difference in the generated code, the async version will be turned into a state-machne. Not really something to worry about.
Since these methods do nothing else it's hard to prefer one over the other. The first (simplest) one will do.
await will actually wait for the task to finish and return the result. When you return a task its returning a handle to the Task which will complete in future, hence you await on the task to get the results.
The DoSomething()
will be executed when asked by the caller.
So it will be executed when you will do await DoSomething();
. But you can store the task in a variable and execute it later :
var task = DoSomething();
// Do stuff
await task();
With DoSomethingAsync
, it will be executed when called. Because it's asynchronous.