Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I just started playing around with async/await in .Net 4.5. One thing I'm initially curious about, why is the async keyword necessary? The explanation I read was that it is a marker so the compiler knows a method awaits something. But it seems like the compiler should be able to figure this out without a keyword. So what else does it do?

share|improve this question
1  
Here's a really good blog article (series) that goes into some detail describing the keyword in C# and related functionality in F#. –  paul Feb 18 '13 at 21:52
    
I think it's mainly a marker for the developer, and not to the compiler. –  CodesInChaos Feb 18 '13 at 21:57
4  
This article explains it: blogs.msdn.com/b/ericlippert/archive/2010/11/11/… –  svick Feb 18 '13 at 21:58
    
@svick thanks, this is what I was looking for. Makes perfect sense now. –  ConditionRacer Feb 18 '13 at 23:30

2 Answers 2

it changes the method from a normal method to a object with callback which requires a totally different approach for code generation

and when something drastic like that happens it is customary to signify it clearly (we learned that lesson from C++)

share|improve this answer
    
So this is really something for the reader/programmer, and not so much for the compiler? –  ConditionRacer Feb 18 '13 at 21:34
    
@Justin984 it definitely helps to signal to the compiler something special needs to happen –  ratchet freak Feb 18 '13 at 21:37
    
I guess I'm curious why the compiler needs it. Couldn't it just parse the method and see that await is somewhere in the method body? –  ConditionRacer Feb 18 '13 at 21:39
    
it helps when you want to schedule multiple asyncs at the same time so they don't run one after another but concurrently (if threads are available at least) –  ratchet freak Feb 18 '13 at 21:45
    
@Justin984: Not every method returning Task<T> actually has the characteristics of an async method - for example, you might just run through some business/factory logic and then delegate to some other method returning Task<T>. async methods have a return type of Task<T> in the signature but don't actually return a Task<T>, they just return a T. In order to figure all of this out without the async keyword, the C# compiler would have to do all sorts of deep inspection of the method, which would probably slow it down quite a bit, and lead to all manner of ambiguities. –  Aaronaught Feb 18 '13 at 23:58

The whole idea with keywords like "async" or "unsafe" is to remove ambiguity as to how the code they modify should be treated. In the case of the async keyword, it tells the compiler to treat the method modified as something that does not need to return immediately. This allows for the thread where this method is used to continue without having to wait on the results of that method. It's effectively a code optimization.

share|improve this answer
    
I'm tempted to downvote because while the first paragraph is correct, the comparison to interrupts is incorrect (in my informed opinion). –  paul Feb 18 '13 at 22:01
    
I see them as somewhat analogous in terms of purpose but I'll remove it for the sake of clarity. –  World Engineer Feb 18 '13 at 22:04
    
Interrupts are more like events than async code in my mind -- they cause a context switch and run to completion before normal code is resumed (and the normal code might be completely ignorant of it running, too), whereas with async code the method might not have completed before the calling thread is resumed. I see what you were trying to say w.r.t. different mechanisms requiring different implementations under the hood, but interrupts just didn't jibe with me to illustrate this point. (+1 for the remainder, btw.) –  paul Feb 19 '13 at 14:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.