Main not running?
learning c# i have int Main(){helloworld(not literally like that but int Main is)}, when i build and run it does nothing
9 Replies
It should be public static afaik
type "psvm" then double tab
the confusion is coming from a feature called top level statements, basically you get a special file where you can just start writing code and it automatically puts it in a Main method behind the scenes
what you've done here is actually create a second Main method, but since it's not called nothing happens
$tls
$toplevelcode
$toplevelcode
Traditionally in C#, the main entry point of any program would be a static
Main
method (see $mains): In C# 9, a feature called top-level statements was added which allows the static Main
method to be omitted in favor of brevity and simplicity, and in C# 10, this was made part of the default console application template:
If you are following a tutorial or example code and your default template looks different, you can still write the exact same code as if you were writing it inside the static Main
method. In addition, you can still write static methods inside top-level code. You can however not use top-level code in more than one file.
If you wish to use the old traditional entry point, you can still use a Program
class with a static Main
method.
https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/program-structure/top-level-statementsTop-level statements - programs without Main methods - C#
Learn about top-level statements. You can create programs without the ceremony of a Program class and a Main method.
ah
normally you can't define methods outside of a class, but because of top level statements it interprets you doing that as making this file a top level statements file
ah