Why is the application entry point a static method?

Asked

Viewed 488 times

14

A declared static method means that it belongs to the type rather than the object instance.

In a C#console application, the entry point is static:

static void Main(string[] args) { ...  }

This is not only of C#, in Java is the same concept:

public static void main(String[] args) { ... }

It is also not limited to console applications. A WPF project has a static entry point.

In the questions that already exist on the subject is not explained about the static beyond its obligation without justification and definition:

Why application entry points are static?

The application itself, initially, is not an object instance of the type Application or ConsoleApplication (or whatever name I gave to the class that contains the input method), for example?

  • 3

    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.

  • 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.

  • The main cannot have more parameters like static main(String[] args, int x)? @Anthonyaccioly

  • 1

    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.

  • 1

    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 method static 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.

Show 1 more comment

1 answer

12


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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.