atoMerz
atoMerz

Reputation: 7672

Why should a void method be async to be able to await?

Assume I have a method that has void return type and that I need to await on an operation in this method.

public void someOperation()
{
    //dostuff
    var res = await someOtherOperation();
    //do some other stuff that needs res.
}

When I tried to compile this code I got the error saying someOperation has to be declare async. I don't understand why. I understand why it would if the method had a return value, but not here when it's void. or even in the case when the awaiting operation has no effect on the return value of method.
This has already been asked as a part of this question but I didn't find the answer I was looking for. This guy merely mentions:

The async keyword enables the await keyword. So any method using await must be marked async.

Upvotes: 4

Views: 1346

Answers (3)

Stephen Cleary
Stephen Cleary

Reputation: 457207

Asynchronous methods must be marked async. The async and await keywords are paired together, primarily for backwards-compatibility reasons; await was a valid identifier (not a keyword), so when it was added to the language, they also added the async keyword.

Alternatively, they could have gone with a multi-word keyword for await, such as await for. However, the language design team decided to go with the pairing, since it clearly and explicitly marks all async methods - it's easier for both compilers and humans to parse.

I have a blog post on the subject, with links to Eric Lippert's definitive blog post as well as discussions in blog comments, Channel9 forums, MSDN forums, and of course right here on Stack Overflow.

Note that the natural return type of an async method without a result value is Task, not void. void is an unnatural return type for async methods. So your default reaction when seeing the "await operator must be within an async method" error should be to mark it async and change the return type from void to Task:

public async Task someOperationAsync()
{
  //dostuff
  var res = await someOtherOperationAsync();
  //do some other stuff that needs res.
}

This follows one of the best practices in my MSDN article on the subject: avoid async void. async void was allowed by the language designers for event handlers (or code that is logically an event handler, like ICommand.Execute); any other usage is going to cause problems.

Upvotes: 11

Hui Zhao
Hui Zhao

Reputation: 675

Because they must work in pair in C#. It is the rule.

The “async” keyword enables the “await” keyword in that method and changes how method results are handled. That’s all the async keyword does! It does not run this method on a thread pool thread, or do any other kind of magic. The async keyword only enables the await keyword (and manages the method results).

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1503290

I don't understand why.

Because you need to tell the compiler that you're trying to write an async method. That's exactly what the document you're quoting means.

The compiler could infer that from whether or not your method includes any await expressions - but that would mean you could comment out one line of a method and the whole method's behaviour would radically change. (Think about exception handling, for example...)

It also improves readability - it means anyone reading your code knows this is an async method right from the start.

So basically, the answers are:

  • You need to because the language specification requires you to. The compiler is implementing the language specification.
  • The language specification requires you to because it reduces the element of surprise.

Upvotes: 11

Related Questions