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

前端 未结 10 1154
梦毁少年i
梦毁少年i 2021-02-06 20:32

What is the difference between

public static void Main()

and

private static void Main()

in a C# console appli

相关标签:
10条回答
  • 2021-02-06 21:29

    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?

    0 讨论(0)
  • 2021-02-06 21:31

    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.

    0 讨论(0)
  • 2021-02-06 21:33

    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.

    0 讨论(0)
  • 2021-02-06 21:34

    Based on access level.

    private--> access to own class
    public --> open to alll
    
    0 讨论(0)
提交回复
热议问题