Why is the main method closed?

The new console project template creates the Main method as follows:

class Program { static void Main(string[] args) { } } 

Why is it necessary that neither the Main method nor the Program class be published?

+40
syntax methods c #
Jun 24 '10 at 13:07
source share
4 answers

The program entry point is marked with the IL .entrypoint directive. It does not matter whether the method or class is publicly available or not, all that matters is this directive.

+43
Jun 24 '10 at 13:11
source share

The Main method should not be called by anyone.

It is actually marked as an entry point for execution in the EXE itself and, therefore, by default does not have external callers.

If you WANT, you can open it for a call by marking public , for example. if you turn the console application into an API.

+18
Jun 24 '10 at 13:11
source share

An open or closed keyword does not matter in this case, it completely depends on the use and volume of the application. Use the keywords below in different scenarios.

1) Public. If we want to initiate an entry point with any external program, you may need to make it publicly available so that it is accessible. 2) Private. If we know that there is no external use for the application, then it is better to make it private, so the external application will not be able to receive it.

0
Feb 19 '14 at 12:47
source share

Yes. You can mark the main() method as public, private, or protected. If you want to initiate an entry point with some external program, you may need to make it publicly available so that it is accessible. You can mark it as private, if you know that the application is not used externally, then it is better to make it closed so that external applications do not access it.

 public class MainMethod { private static void Main(string[] args) { Console.WriteLine("Hello World !!"); } } 
-one
Dec 13 '18 at 7:40
source share



All Articles