Browsing through the channel 9 msdn videos I found the following unanswered comment and was hoping someone could possibly explain it?
I dont get the point of the async keyword. Why not just allow the await keyword anytime the method returns Task, just like iterators can yield return on any method that returns an IEnumerable.
I'm sure there is a good reason, I'd just like to understand why the above suggestion was not possible.
For me, the most compelling reason is that the meaning of the
return
statement changes when a function becomesasync
. Withoutasnyc
return x
means "return a task with the valuex
", and with async it means "set the result of the task tox
.It was introduced mainly to avoid backward compatibility issues. If the
async
-ness of a method must be inferred by the compiler (that would be through the detection ofawait
keywords), then there are subtle scenarios where existing code would suddenly be treated differently, notably when you have identifiers (variable or function names calledawait
).A full explanation is here: http://blogs.msdn.com/b/ericlippert/archive/2010/11/11/whither-async.aspx
I think perhaps this article covers the reasoning:
http://blogs.msdn.com/b/ericlippert/archive/2010/11/11/whither-async.aspx
The first paragraph states:
It concludes:
The short of it is backwards compatibility.
Further reading:
http://blogs.msdn.com/b/ericlippert/archive/2010/10/29/asynchronous-programming-in-c-5-0-part-two-whence-await.aspx
I wrote up a summary of async/await keyword questions on my blog a while ago.
Here's the conclusion of the section "Inferring
async
":