skip to Main Content

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

Answers


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

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search