Why does the async keyword exist
Asked Answered
M

4

45

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.

Macmullin answered 10/2, 2012 at 9:46 Comment(5)
My spidey-sense is tingling... Jon Skeet is on his way! While we all wait, a little further reading: blogs.msdn.com/b/ericlippert/archive/2010/10/29/…Neckband
Needing two keywords in separate locations to implement one new feature seems like a bad design choice. The fact that quite a few people are asking this question means (to me) that the wrong choice was made. Reading through the all the reasoning in the blogs, I can follow the logic, but compromise isn't always the best plan. Sometimes it's better to break some code and move on.Gastrology
I personally think the safest choice was made. If all that stands between breaking code and not breaking code is a simple keyword, then that isn't too onerous a task. People might be vocal about it, but one can assume they won't experience the backwards compatibility problem. Lots of people who agree or are impartial are not vocal.Neckband
@Adelphus: Well then, let's use science. You create your own implementation that doesn't use the async keyword, and we'll see how many questions that provokes.Top
Suggestion to replace it with a compiler option and [EnableAwait(true|false)] attribute: visualstudio.uservoice.com/forums/121579-visual-studio/…Verrucose
B
25

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 of await keywords), then there are subtle scenarios where existing code would suddenly be treated differently, notably when you have identifiers (variable or function names called await).

A full explanation is here: https://learn.microsoft.com/en-us/archive/blogs/ericlippert/asynchrony-in-c-5-part-six-whither-async

Benzoate answered 10/2, 2012 at 9:58 Comment(1)
Url seams broken? I think this one is the one being referred to: learn.microsoft.com/en-us/archive/blogs/ericlippert/…Commonplace
N
15

I think perhaps this article covers the reasoning:

https://learn.microsoft.com/en-us/archive/blogs/ericlippert/asynchrony-in-c-5-part-six-whither-async

The first paragraph states:

A number of people have asked me what motivates the design decision to require any method that contains an "await" expression to be prefixed with the contextual keyword "async".

It concludes:

That's a whole lot of pros and cons; after evaluating all of them, and lots of playing around with the prototype compiler to see how it felt, the C# designers settled on requiring "async" on a method that contains an "await". I think that's a reasonable choice.

The short of it is backwards compatibility.

Further reading:

https://learn.microsoft.com/en-us/archive/blogs/ericlippert/asynchrony-in-c-5-part-one

Neckband answered 10/2, 2012 at 9:58 Comment(5)
+1. Quoting Eric Lippert is the next best thing to being Eric Lippert :)Downy
I would just add that I dearly wish that the C# 2 designers had decided to put "iterator" or some similar keyword at the beginning of every method that is intended to be an iterator. It is very vexing to have to parse potentially the entire method body before you know whether it is an iterator block or not.Top
Not to mention it'd allow anonymous methods to be iterators too.Maclay
The link to Eric Lippert's blog, above, is broken. THe new location of the first of that series of posts is learn.microsoft.com/en-us/archive/blogs/ericlippert/….Dodona
@ElroyFlynn Thanks for the catch, I've updated the post with your link :-)Neckband
D
11

For me, the most compelling reason is that the meaning of the return statement changes when a function becomes async. Without asnyc return x means "return a task with the value x", and with async it means "set the result of the task to x.

Dasheen answered 10/2, 2012 at 10:5 Comment(0)
S
3

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":

Eric Lippert has the definitive post on the subject. It's also been discussed in blog comments, Channel9, and forums.

To summarize, a single-word await keyword would be too big of a breaking change. The choice was between a multi-word await (e.g., await for) or a keyword on the method (async) that would enable the await keyword just within that method. Explicitly marking methods async is easier for both humans and computers to parse, so they decided to go with the async/await pair.

Spring answered 10/2, 2012 at 13:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.