Say goodbye to static void main in c#9 & .NET 5

Say goodbye to static void main in c#9 & .NET 5

With .NET 5 projected in November 2020, the compiler will determine the new C# 9 language version as it’s default, allowing some very nice improvements, one of which gives us the ability to remove boilerplate code which serves no benefit to a developer other than ‘It is required, in order to work’.

When you first started to learn c# you probably saw the below on a tutorial or exercise and thought, ‘What is going on here? static void main, what?’.

using System;
class Program
{
    static void Main()
    {
        Console.WriteLine("Hello World!");
    }
}

C# 9 allows us to remove the static void Main() method & the class definition, as a result leaving us with a much neater implementation, without the unnecessary indentation and boilerplate code.


Check out the below!

using System;

Console.WriteLine("Hello World!");

I think it’s fair to say it will be extremely strange seeing console apps without the static void declaration but it’s a great visual improvement & I can’t wait to start making use of it in .NET 5.

For a list of further features planned for C#9 check out this Microsoft blog https://devblogs.microsoft.com/dotnet/welcome-to-c-9-0