What is the difference between
public static void Main()
and
private static void Main()
in a C# console appli
Aside from the normal public and private access modifier functionality, nothing. Both are valid entry points.
See: Why is the entry point allowed to be private? and Why is Main method private?
To act as the start point in your application, the Main
method is not required to be public
.
If you did decide to make it public
, it would be possible for it to be called from other classes or assemblies. Typically you will not need to do this, so you can keep it private
.
One possible use case for making it public
would be to allow automated tests to invoke it.
For example when you want add entry point that can call from outside a class or assembly you should set public
but if it is not importatnt use private
.
Based on access level.
private--> access to own class
public --> open to alll