What is the difference between public static void Main() and private static void Main() in a C# console application?

What is the difference between

public static void Main()

and

private static void Main()

in a C# console application? Specifically as it pertains to the Main() method (I understand the differences between public and private).


Solution 1:

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.

Solution 2:

The difference between both is the only difference in public and private access modifiers because both are valid.It totally depends on the usage of application which one to use.

If you want to initiate entry point by any external program, (ie use as API, for testing purpose) then you might need to make it public so it is accessible.

public

If you know there is no external usage for the application then it is better to make it private so no external application get access to it.

private

Solution 3:

For most purposes it will make no difference. Microsoft advocates making Main private.

The only real value in doing this (as far as I am aware) is that it will prevent the Main method from being invoked directly by another application's codebase.

A good discussion of it is available here