Search code examples
c#staticmain-method

Why is a program main method static?


I always thought (assumed) that the Main method was static because you cannot have multiple instances of it (Correct me if that's wrong). The Main method is the start point of your program and thus you can have only one.

So if I have

class Program
{

   static void Main(String[] args)
   { // something
   }

}

class OtherClass
{

   void Test()
   { 
      Program p1 = new Program();
      Program p2 = new Program();
      Program p3 = new Program();
      Program p4 = new Program();

   }

}

all instances of Program will share the same Main method and so there will always be one start point.

Am I correct? Because I just googled this out of curiosity and found DIFFERENT answers to it on the Internet.

Is this explanation ALSO correct for the main method being static?


Solution

  • If the entry point method were not static, someone would need to create an object first. The question is then: create an object of which class?

    I always thought (assumed) that the Main method was static because you cannot have multiple instances of it

    You can't have instances of methods. Methods reside in the Code section of a DLL and are not copied to the heap. You can only have multiple threads running in the same method.

    The Main method is the start point of your program and thus you can have only one.

    As before: If we consider signatures, there is only one method, independent if it's static or not, because they are not instantiated.

    all instances of Program will share the same Main method ...

    Depends on what you understand by the term "share". All objects will have the method, yes.

    ... and so there will always be one start point.

    The reasoning behind it is wrong. You have many instances of Program but that doesn't matter to the amount of methods.