Why is Main method private?

前端 未结 4 1326
抹茶落季
抹茶落季 2020-11-29 06:28

New console project template creates a Main method like this:

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

Why is it th

4条回答
  •  抹茶落季
    2020-11-29 06:48

    Yes. You can mark the main() method as public, private or protected. If you want to initiate the entry point by any external program then you might need to make it public so it is accessible. You can mark it as private if you know there is no external usage for the application then it is better to make it private so no external application access it.

    public class MainMethod
    {
        private  static void Main(string[] args)
        {
            Console.WriteLine("Hello World !!");
        }
    }
    

提交回复
热议问题