Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
3.57/5 (7 votes)
See more:
Why we have to declare main function as static?


static void main()
{


}
Posted
Comments
Abhinav S 15-Mar-11 2:05am    
5 for marking the question as answered. Many OP's dont do that!
Tarun.K.S 15-Mar-11 5:57am    
Lol right!
sairam.bhat 15-Mar-11 2:12am    
k thanks for 5

You need an entry point into your program. And static makes sure you can do this without creating an instance of the entry point class.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 15-Mar-11 1:00am    
Basically correct, a 5, but needs some explanation; would you see mine?
--SA
Abhinav S 15-Mar-11 1:02am    
Thanks for the 5.
Albin Abel 15-Mar-11 1:57am    
This is correct. My 5+
Abhinav S 15-Mar-11 2:04am    
Thanks Albin.
sairam.bhat 15-Mar-11 2:49am    
mine also 5+
If it is not static, it is the instance one, so: what should create the very first instance? Not having any instance in the very beginning is very simple and natural convention. Do you know Occam Razor and why it is important: http://en.wikipedia.org/wiki/Occam's_razor[^]?

—SA
 
Share this answer
 
v2
Comments
Abhinav S 15-Mar-11 1:02am    
:). 5.
Sergey Alexandrovich Kryukov 15-Mar-11 1:06am    
Thank you.
--SA
Albin Abel 15-Mar-11 1:58am    
This answer gives the exact reason. My 5+
Sergey Alexandrovich Kryukov 15-Mar-11 12:38pm    
Thank you, Albin
--SA
Dalek Dave 15-Mar-11 5:49am    
Excellent answer.
The only reason is because that's what the designers of the language decided to be what to look for as an entry point for your program. They could just as well have used a totally different approach to find the entry point, e.g. using meta data, or instantiating an object for you. The main reason for naming it void main() is that it's intuitive for users coming from other languages, and then making it static makes sense, as described in Abhinavs and SAKs posts.
 
Share this answer
 
v3
Comments
Nuri Ismail 15-Mar-11 6:11am    
Excellent comment, 5.
Espen Harlinn 15-Mar-11 12:21pm    
True enough, my 5 - but I somehow get the feeling that meta data etc. is more than OP can handle at this point ...
Niklas L 15-Mar-11 15:09pm    
You might be right... Just wanted to point out that the entry point for a program is somewhat different than other constructs in the way the compiler handles it.
Espen Harlinn 15-Mar-11 15:29pm    
Bra svar :)
Niklas L 15-Mar-11 15:47pm    
:)
Because C# (like, for instance, Java) hasn't functions, it has methods. You know a method belongs to an object and you need an instance of the object in order to call it.
So, in principle you couldn't have an entry point in C#, because you haven't pre-built object instances.
The very escape to the above rule are 'static methods': they resembles 'ordinary function' because they don't need an instance of the object to be called.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900