Initially the application is not an instance of anything. And in a way this explains why the method is static.
It could be an instance method, there’s nothing technically to stop you doing it that way, it’s just the way they chose it, maybe because it’s always been that way in other existing languages before C#, most of which don’t even have the concept of classes and instantiation of objects.
Could more, could let define another method, maybe even with another signature, even passing other arguments. Remember you need to tell me what kind you should use as entry point. And may have Main()
in any type without being an entry point.
Android instantiates objects and does what you need. For the operating system model is more interesting. But traditional operating systems have always had a unique way of starting an application. So I would say that they chose this way even for historical reasons, everyone was used to doing this.
In nonobject-oriented languages a function is used as an input point and the static method is the same as a function.
I think I understand the use of the static method helps to understand that decision. Contrary to what the ideologues of OOP preach, a static method is always simpler to deal with and it should always be preferred where it fits (it does not fit in a lot of situation). The Main()
is a case that fits like a glove.
I do not see as much difficulty as they said in the comments, but it makes some sense. Simplification seems to me the best argument to avoid this functionality that would bring little or no advantage. Language developers are pragmatic and analyze the costs of implementing something, of complicating the language and the benefits it will bring.
One point I think you can take into consideration is that you might want to call this Main()
within your application. There you need to instantiate another object, because the object that started the application is owned by CLR (unless you pass it around and around in the application). If it is another instance, although physically it will call the same method, it is accessing another object, with possibly different data, it may not be what you want.
In C# you can use 4 different signatures (not simultaneously):
static void Main();
static void Main(string[] args);
static int Main();
static int Main(string[] args);
I put in the Github for future reference.
In C# 7.1 can even more because the Main()
can be asynchronous.
In C# 9 there is no need to have this method made explicit for scripts simple. It’s not that it’s no longer necessary, the compiler puts it to you. Just do it for code throw away.
If the
main
not being static, how would you deal with class builders? What if the constructor has parameters? What if the class has multiple constructors? I believe the point of designers of these languages was to define a simple "contract" for the application entry point.– Anthony Accioly
I believe it is for convenience and possibly semantics. The
main
is only the entry point of the program and belongs to a class that (usually) has no real representativeness: it only exists to have the method (it could be only a function). And if the class has representativeness, the instance can be done within this method without problems.– Woss
The
main
cannot have more parameters likestatic main(String[] args, int x)
? @Anthonyaccioly– vinibrsl
In java, to my knowledge, the fact of receiving an array already means that you can pass as many parameters as you want, now the type, is only string even, if you need another type, you have to cast in main after recovering the parameters.
– user28595
Related: What public Static void main(String[] args means)?
– Woss
Not in Java (and not in C#). But the point of my initial comment goes beyond the type and amount of parameters. When we introduce OO into the equation we have to solve all the problems of constructors, destructors, overloading, Overriding, etc, etc, etc. Java does not have a type Singleton (
object
) well behaved or functions standalone. A methodstatic
seems to me to be the most convenient way to secure this contract (basically procedural) in an OO language that does not have more modern mechanisms.– Anthony Accioly