C
C#4mo ago
Momma Luxara

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
Momma Luxara
Momma LuxaraOP4mo ago
// See https://aka.ms/new-console-template for more information
int Main()
{
Console.WriteLine("Hello, World!");
return 0;
}
// See https://aka.ms/new-console-template for more information
int Main()
{
Console.WriteLine("Hello, World!");
return 0;
}
Servator
Servator4mo ago
It should be public static afaik type "psvm" then double tab
Jimmacle
Jimmacle4mo ago
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
MODiX
MODiX4mo ago
$toplevelcode
Jimmacle
Jimmacle4mo ago
$toplevelcode
MODiX
MODiX4mo ago
Traditionally in C#, the main entry point of any program would be a static Main method (see $mains):
public static class Program
{
public static void Main()
{
Console.WriteLine("Hello, world!");
}
}
public static class Program
{
public static void Main()
{
Console.WriteLine("Hello, world!");
}
}
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:
Console.WriteLine("Hello, world!");
Console.WriteLine("Hello, world!");
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-statements
Top-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.
Momma Luxara
Momma LuxaraOP4mo ago
ah
Jimmacle
Jimmacle4mo ago
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
Momma Luxara
Momma LuxaraOP4mo ago
ah
Want results from more Discord servers?
Add your server