Reputation: 92
Suppose I have the following method:
public async Task<bool> DoSomeStuffWithAsync()
{
//do some stuff without calling await
return true;
}
and I call it like this:
await DoSomeStuffWithAsync();
Does this run any differently to this method:
public bool DoSomeStuff()
{
return true;
}
being called like this:
DoSomeStuff();
I am thinking that because DoSomeStuffWithAsync
doesn't actually call await
in its code that it will still run the same as DoSomeStuff
.
Is there any benefit structuring my method like DoSomeStuffWithAsync
?
Upvotes: 0
Views: 988
Reputation: 181
Your methods return different things.
DoSomeStuffWithAsync() returns a Task<bool>
. You can call it without the 'async" keyword - synchronously - the compiler will return bool wrapped in an instance of Task<bool>
once the method is complete - it does that behind the scenes. And another "difference" is how you handle the returned value...
var task = DoSomeStuffWithAsync(); // Synchronous call
Console.WriteLine(task.Result); // You must unpack the bool result...
Your other method just returns a bool - it's just a standard synchronous call/return...
var result = DoSomeStuff(); // Synchronous call
Console.WriteLine(result); // Just use the result
From a behavior point-of-view, to answer your specific question, both behave synchronously. Another way to look at it:
var act = new Func<Task<bool>>(async () => await DoSomeStuffWithAsync());
var result = act(); // will complete when DoSomeStuffWithAsync completes
Hope this answers your question.
Upvotes: 1
Reputation: 4002
In short terms async
keyword does not make a method to run asynchronously. It only allows the method to call await
keyword inside. That's where the magic happens. In your case you don't have any await
keyword anywhere which indicates your method does not indeed run asynchronously.
Upvotes: 1
Reputation: 387507
Note that using async
for a method that does not use await
is considered a bad style. If you have a synchronous method that returns a task, you should use Task.FromResult
instead:
public Task<bool> DoSomeStuffWithAsync()
{
return Task.FromResult(true);
}
But yeah, that’s all this is: A synchronous method that returns a task. There is nothign asynchronous about it, so this does not actually run any differently than normal synchronous code that just returns a bool.
Is there still a benefit to returning a task instead of the value directly? That depends. Sometimes, you may have an asynchronous interface you need to implement but you don’t actually have an asynchronous implementation for it. But if you don’t have to do this and if you also don’t consider changing your implementation eventually to actually do something asynchronously, then there’s no benefit in making your method return a task.
As for the call, await DoSomeStuffWithAsync()
will run DoSomeStuffWithAsync()
and then await the task it returns. Since DoSomeStuffWithAsync()
is synchronous, the task will return directly, and since the task is already completed, await
will be able to directly continue. So this will be very similar to calling the method synchronously. The presence of that await
does have an impact on the runtime behavior though, since this will make that calling method actually asynchronous (even if the called code is synchronous).
But it’s up to you to decide whether that’s appropriate in your case. In general though, you should probably avoid making this appear asynchronous when there isn’t a real asynchronous process involved.
Upvotes: 4