How to enable auto-generating main method when creating console application in VS Code in C#

100 Views Asked by At

In .NET 6.0, the Main method is simplified by removing and main method. Now it's possible to start writing code directly without it.

But I want to enable it back and don't want to write it by hand every time when new project is created. I'm using VS Code, but I couldn't find any straight way to accomplish this.

Is there any option for this or any shortcut like writing "prop" to create property quickly.

2

There are 2 best solutions below

2
Sam B On BEST ANSWER

With your cursor on the code that it starts with, you can press Ctrl+., and click "Convert to 'Program.Main' style program" example

1
David On

When creating a console application project on the command line, there are options you can specify. By default:

dotnet new console

Will create a console application in the "newer" (as of .NET 6) style of allowing top-level statements. You can suppress this on the command line when creating the project:

dotnet new console --use-program-main

This would create the project with an explicit class and method entry point.