Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the async keyword exist

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.

like image 508
Maxim Gershkovich Avatar asked Feb 10 '12 09:02

Maxim Gershkovich


People also ask

Why do we use async keyword?

It allows a program to run a function without freezing the entire program. This is done using the Async/Await keyword. Async/Await makes it easier to write promises. The keyword 'async' before a function makes the function return a promise, always.

What does async keyword in a method signify?

The async keyword is contextual in that it's a keyword only when it modifies a method, a lambda expression, or an anonymous method. In all other contexts, it's interpreted as an identifier.

What is the purpose of async keyword in VB net?

Async methods are intended to be non-blocking operations. An Await expression in an async method doesn't block the current thread while the awaited task is running. Instead, the expression signs up the rest of the method as a continuation and returns control to the caller of the async method.

What is the benefit of using async?

with async / await , you write less code and your code will be more maintainable than using the previous asynchronous programming methods such as using plain tasks. async / await is the newer replacement to BackgroundWorker , which has been used on windows forms desktop applications.


2 Answers

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://docs.microsoft.com/en-us/archive/blogs/ericlippert/asynchrony-in-c-5-part-six-whither-async

like image 197
jeroenh Avatar answered Sep 21 '22 16:09

jeroenh


I think perhaps this article covers the reasoning:

https://docs.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:

http://blogs.msdn.com/b/ericlippert/archive/2010/10/29/asynchronous-programming-in-c-5-0-part-two-whence-await.aspx

like image 20
Adam Houldsworth Avatar answered Sep 20 '22 16:09

Adam Houldsworth