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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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
staticto 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.