I'm aware that the use of async/await does not actually create a new thread -- but I'm struggling to figure out what could lead to the following behavior ->
// .NET 5
using System;
using System.Threading.Tasks;
var f = FakeIOOperation();
Console.WriteLine("then I am called after the await Task.Delay() relinquishes control to the calling method...");
while(true){Console.Write("");}
Console.WriteLine(await f);
async Task<string> FakeIOOperation()
{
Console.WriteLine("Firstly, I am called...");
await Task.Delay(5000); // Simulate something like a 5000ms API call
Console.WriteLine("I shouldn't be printed");
return "Data";
}
Why does the "I shouldn't be printed" line get printed out when halting the main thread using the infinite while loop? I was under the impression that anything after the await Task.Delay(5000) is only run once we evaluate the result of the Task. Is a new thread actually being created? And why?