Why must "await" be inside an "async" method?

async-await, c#

Solution

The entire point of the `async` keyword is to enable the `await` keyword.

I collected a number of "why do the keywords work that way?" questions on my blog. The question of "why can't `async` be inferred by the presence of `await`?" was answered conclusively by Eric Lippert on his blog. In short, two reasons: backwards compatibility and code readability.

Problem

Possible Duplicate: Why does the async keyword exist I have two methods. One is a normal method (`MyMethod`) and one is an async method (`MyMethodAsync`). I get a compilation error. ``` static string MyMethod() { var result = await MyMethodAsync(); // compile error here return result; } async static Task<string> MyMethodAsync() { /** perform my logic here... **/ Thread.Sleep(1000); return "yes"; } ``` The error message is The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'. I'm confused. When I use the `await` keyword, the calling thread will be suspended and wait for task completion. So once `await` is in use, the method is no longer an async method. Right? Remark: I know I should put the logic in `MyMethod` and `MyMethodAsync` calls `MyMethod` to achieve what I want.

Original source

Related problems