Why is the main method entry point in most C# programs static?
-
This is a great question, for which there may be no really satisfactory answer. Lots of things run before Main gets called, and these can include as many object constructors as you'd like. But still, I think that if the constructor for the Program object gets an out of memory exception, then your computer is having a *really* bad day! – Jeffrey L Whitledge Mar 02 '10 at 22:03
-
5Because you don't want to frighten C++ and Java programmers too much. – zaratustra Mar 02 '10 at 22:38
8 Answers
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.

- 733,204
- 149
- 1,241
- 1,454
-
9
-
3@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 Mar 02 '10 at 21:44
-
2The 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 Mar 02 '10 at 21:57
-
2The 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 Mar 02 '10 at 22:09
-
Probably it just didn't occur to the developers that Program() could have the main functionality. – zaratustra Mar 02 '10 at 22:40
-
@zarawesome: they used to have one or two smart people working at Microsoft. You should substantiate "probably just didn't occur". – John Saunders Mar 03 '10 at 14:14
-
@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 Mar 03 '10 at 17:41
-
@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 Jun 14 '10 at 22:05
-
If the framework were to always call `new Program().Main(args)` and, by definition, you have no control over what that `Program` object is or how it's constructed, is this really any different than `Main` being static? Which is far simpler, more logical, and easier to implement? Why do I want the language to decide what sort of objects should be instantiated for me? – Lightness Races in Orbit Jun 13 '11 at 09:06
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?

- 647,829
- 179
- 1,238
- 2,067
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.

- 25,014
- 6
- 48
- 78
Because otherwise it would have to create an object, and running the constructor could cause negative side effects.

- 16,338
- 3
- 63
- 73
How could you create your class instance before main otherwise?

- 35,022
- 6
- 77
- 199
-
3The 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 Mar 02 '10 at 21:58
Static methods can be executed without creating an instance. By convention, they have the main
method as the default method to call.

- 39,895
- 28
- 133
- 186
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

- 21,468
- 17
- 69
- 94
for every objects of a class contains main method and other methods and variables , there are separate copies of each variable and methods contained by all objects but a main class copy is only one between them and so to make a copy between number of objects we have to make main method as static.

- 1