@Dan Dinu's answer from a previous question regarding asynchronous programming in C# provides a useful minimal example, which I have adapted as follows:
// From https://stackoverflow.com/questions/14455293/how-and-when-to-use-async-and-await
using System;
using System.Threading.Tasks;
namespace minimal_async_await_SE
{
    internal class Program
    {
        public static async Task MyMethodAsync()
        {
            Task<int> longRunningTask = LongRunningOperationAsync();
            // independent work which doesn't need the result of LongRunningOperationAsync
            // can be done here
            Console.WriteLine("Independent work");
            //Call await on the task
            int result = await longRunningTask;
            Console.WriteLine(result);
        }
        public static async Task<int> LongRunningOperationAsync()
        {
            await Task.Delay(1000);
            return 1;
        }
        static void Main(string[] args)
        {
            MyMethodAsync();
            Console.WriteLine("Returned to Main");
            //Console.ReadKey();
        }
    }
}
If I uncomment line 32, I get the following expected result:
Independent work
Returned to Main
1
Basically:
- Maincalls- MyMethodAsync
- MyMethodAsynccalls- LongRunningOperationAsync
- LongRunningOperationAsyncthen calls- Task.Delay, but- awaits it which suspends further evaluation of the enclosing method- LongRunningOperationAsync, returning control to the caller, namely- MyMethodAsync.
- MyMethodAsyncprints out- "Independent work".
- MyMethodAsyncattempts to assign the result of- LongRunningOperation toresult- butawait- s it, suspends evaluation of the enclosingMyMethodAsync- , and returns control of the program back toMain`
- Mainprints out- "Returned to Main"
- Task.Delay(1000)in- LongRunningOperationAsync()completes
- A new thread is  spawned, In the caller of LongRunningOperationAsync(MyMethodAsync) the integer1is assigned toresult.
- Evaluation of MyMethodAsynccompletes, andMyMethodAsyncprints out the value ofresult
- Control is given back to Main, which suspends evaluation until the user enters a key viaConsole.ReadKey
Firstly, is my understanding of how this program evaluates correct? Secondly, why is it that when I comment Console.ReadKey, I get the following unexpected result?
Independent work
Returned to Main
Does the Main method not wait for all threads to get evaluated before exiting out of the program? Why or why not?
 
    