C# 7.1 – Directly await tasks in Main

Starting in C# 7.1, you can directly await in Main:

static async Task Main(string[] args)
{
	while (true)
	{
		Console.WriteLine("I'm looping");
		await Task.Delay(5000);
	}
}

Previously I would’ve written this code like this:

static void Main(string[] args)
{
	Task.Run(async () =>
	{
		while (true)
		{
			Console.WriteLine("Looping");
			await Task.Delay(5000);
		}
	});
	Console.Read();
}

Leave a Comment