views:

729

answers:

8

Why is the main method entry point in most C# programs static?

+1  A: 

Because otherwise it would have to create an object, and running the constructor could cause negative side effects.

David Morton
+1  A: 

How could you create your class instance before main otherwise?

Vlad
The same way as you call main. You don't - the runtime does a lot of stuff before main, loading classes and creating objects.
Pete Kirkham
+49  A: 

In order to call an instance method you need an instance of an object. This means in order to start your program the CLR would need to create an instance of say Program in order to call the method Main. Hence the constructor of Program would run before Main which defeats the purpose of having a main altogether.

JaredPar
+1 Nicely explained.
Andrew Hare
But this means the static constructor is still called?
Yuriy Faktorovich
@Yuriy, yes it does. It's specifically called out as such in section 10.11 of the C# language spec. I don't know exactly why this decision was made (may have been forced from the CLR level)
JaredPar
The purpose of having a main is to have a defined entry point to the application. Defining the entry point as having the framework call `new Program().Main(args)` instead of the static call wouldn't defeat the purpose at all.
Pete Kirkham
The reason is because that's the way java did it. At the time Java was created, it was still assumed that for many jobs Objects could be forgone. You might have a JVM on a chip with no heap (just stack)--where they don't want to (or can't) instantiate a single class. C# was a pretty direct copy of Java at the time even though it's added more enhancements since, but since C# couldn't run in such an environment it would be plausible to have it use a constructor with (args) as a parameter.
Bill K
Probably it just didn't occur to the developers that Program() could have the main functionality.
zaratustra
@zarawesome: they used to have one or two smart people working at Microsoft. You should substantiate "probably just didn't occur".
John Saunders
@John used to??? :)
JaredPar
@JaredPar: well, I happen to think there still are more than one or two, but the OP seems to have a different opinion. I'm pointing out that, even if the entire company has gone stupid in the last few years, this would have occurred to the one or two they employed back then.
John Saunders
@John, ah I read the comment in the other direction.
JaredPar
@Yuriy Faktorovich Yes but, aren't staic constructors that are called the first thing to be run during execution? Wouldn't it then make sense for the entry point for a program to be a static constructor?
Evan Plaice
+4  A: 

Conceptually you only have one instance of a static. And a static method maps well to the idiom of a single staring point for a program. The language designers could have created a special program class to use with a main method but chose to create a single static function as the entry point. On some levels its really just a design choice.

rerun
A: 

Static methods can be executed without creating an instance. By convention, they have the main method as the default method to call.

fastcodejava
A: 

The .NET runtime calls the Main method. (Note: Main may also be called from elsewhere, e.g. from the code Main() in another method of ExampleClass.) The static keyword makes the method accessible without an instance of ExampleClass. So Main method is an entry point and must be declared static.

Otherwise, the program would require an instance, but any instance would require a program.

To avoid that irresolvable circular dependency main is used as an entry point


reference : http://en.wikipedia.org/wiki/C_Sharp_(programming_language

Asad Butt
+13  A: 

I'd turn the question around. What is the compelling benefit of implementing the feature that allows Main to be an instance method? Features are expensive; if there is no compelling benefit, they don't get implemented.

Do you have a really good reason why Main should be allowed to be an instance method?

Eric Lippert
A: 

how to create shortcut keys in c#

veeramuthu