Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the entry point allowed to be private? [duplicate]

Tags:

c#

.net

How does this actually work? I thought Main was supposed to be "called". But how is that possible if it's marked private?

public class Program
{
    private static void Main()
    {
    }
}
like image 931
Marlon Avatar asked Dec 13 '25 01:12

Marlon


2 Answers

From Jon Skeet on bytes.com:

Basically, the execution of the main method is started by special code within the CLR (or possibly code driving the CLR to start with) which doesn't need to obey the same rules.

Source

Also, there's another question that covers this topic here already.

like image 156
Cᴏʀʏ Avatar answered Dec 14 '25 16:12

Cᴏʀʏ


Following to MSDN the Main method should not be public:

Main is declared inside a class or struct. Main must be static and it should not be public. (In the earlier example, it receives the default access of private.) The enclosing class or struct is not required to be static.

like image 43
MarcinJuraszek Avatar answered Dec 14 '25 15:12

MarcinJuraszek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!