I tried to create public void Main()
in C#; it says no static void Main found.
What exactly does it mean for Main
to be static? I know the code works fine for public static void Main()
.
But why does Main
have to be static
?
I tried to create public void Main()
in C#; it says no static void Main found.
What exactly does it mean for Main
to be static? I know the code works fine for public static void Main()
.
But why does Main
have to be static
?
You need an entry point into your program. Static means that you can call the function without having to instantiate an object/instance of a class. It's a bit "chicken and egg"... you can't instantiate an object before you're inside the program.
A static method can be called without instantiating an object. Therefore main()
needs to be static in order to allow it to be the entry to your program.
As David says, you can just add the keyword static
to the function definition to change it. It's worth looking into static (class) methods vs instance methods, and knowing the difference can be useful at times.