Home > Enterprise >  Why do C Sharp programs created in Visual Studio 2022 have no main method?
Why do C Sharp programs created in Visual Studio 2022 have no main method?

Time:10-09

I'm trying to create a program using OOP in C# with Visual Studio 2022. However normally when you create a class, for example in Java there is a main method that is used to run the program. But in C# and Visual Studio I don't have a main class all I see is: enter image description here

When it's supposed to be this: enter image description here Is there a reason for this?

CodePudding user response:

These are called top-level statements and is a relatively new language feature. The concept is explained well in the documentation: https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/top-level-statements

CodePudding user response:

"Starting in C# 9, you can omit the Main method, and write C# statements as if they were in the Main method" https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/program-structure/main-command-line

CodePudding user response:

You created your project with a top-level statement. If you want to view the Main() method, select "Do not use top-level statements" before creating the Visual Studio project.

See the top-level statement checkbox here

  • Related