1

Does anyone know why it's not possible to mark Main as async?

I've been designing compilers as a case study in the past, but I really can't understand why NOT to make Main as async?

SHM
  • 1,896
  • 19
  • 48
  • possible duplicate of [Async on main method of console app](http://stackoverflow.com/questions/9208921/async-on-main-method-of-console-app) – ta.speot.is Mar 22 '14 at 07:48

1 Answers1

3

In C#, the async keyword essentially just allows you to use the "await" keyword in the code. If there are no awaits, it just executes synchronously.

If there IS an await keyword, then the method blocks until the "awaited" function is complete, and returns control to the caller. However, with the entry point function there is no caller (except the runtime, but you don't ever return control to it) and so using the await keyword doesn't really make any sense.

Since you wouldn't use "await" in the entry point function, there is no reason to mark it async either. C# just happens to enforce this.

The short answer is, your entry point function should always execute synchronously, since it "is" your program (of course there are other parts, but they all start and end with the entry point eventually), and so async/await are not allowed.

MSDN reference for async/await.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117