51

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).

TylerH
  • 20,799
  • 66
  • 75
  • 101
jai
  • 582
  • 1
  • 6
  • 20
  • Not an answer, but note that Microsoft strongly recommends you not make Main() public (partly since it makes little sense) – Michael Edenfield Feb 19 '14 at 16:13
  • 1
    I would argue for `public`. It's oughtn't to be "`private`" because, notionally, the operating system is calling it from outside the class. Those of us with a C background also object to a main function being a method, but that's another issue. – geometrian Feb 20 '14 at 07:25

10 Answers10

63

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.

Ergwun
  • 12,579
  • 7
  • 56
  • 83
  • 2
    Sidenote: creating an empty console application will result in `class Program` which is defaulting to internal class visibility. So apart from the `Main`-method, even the class doesn't need to be public. – Caramiriel Feb 19 '14 at 12:45
22

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

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
  • 2
    -1 By using reflection, you can easily get private methods and fields of other classes. – Sarp Kaya Feb 19 '14 at 12:47
  • 9
    see here [Does reflection breaks the idea of private methods, because private methods can be access outside of the class?](http://stackoverflow.com/a/3304350/821057) – Zaheer Ahmed Feb 19 '14 at 13:03
  • What if the API is using reflection to access main() as it might be a private method? What I meant is, if other applications want to use another application, then these other applications can simply use reflection to avoid the private main method problem. – Sarp Kaya Feb 19 '14 at 13:11
  • 10
    @SarpKaya The point is that a `private` tells you that it is not available for you / you should not call it. That it is possible through a backdoor such as Reflection doesn't change this. – M. Mimpen Feb 19 '14 at 14:39
  • 6
    @SarpKaya: Encapsulation != Security. – Engineer2021 Feb 19 '14 at 14:53
  • +1 for screens :) I'm tired now and don't have time for TL;DR – ALZ Feb 19 '14 at 21:39
  • -1 How does the shell run the program if it's private? – mikek3332002 Feb 20 '14 at 00:36
  • @M.Mimpen I am aware of that, however I did not like parts where "no external application get access to it." and "any external program, (ie use as API, for testing purpose)¨ The answer simply generalizes all API/external programs and assumes that they cannot use any workaround to call a private method. – Sarp Kaya Feb 20 '14 at 07:37
  • @GIJoe, Exactly, I agree with you! The problem is, answer talks about making private methods as a way of security rather than encapsulation. – Sarp Kaya Feb 20 '14 at 07:39
  • first line of answer is, `The difference between both is the only difference in public and private access modifiers` which make it clears restriction for these modifiers are valid here and `any external program, (ie use as API, for testing purpose)` are said for public modifiers. – Zaheer Ahmed Feb 20 '14 at 07:43
  • @SarpKaya Yes, I agree! It has nothing to do with security, simply adding documentation (implicitly) to the API. Though telling that something is breakable is not really interesting, someone could always plug in a debugger and do anything he wants - or use Reflection ;-) – M. Mimpen Feb 20 '14 at 10:03
  • 1
    @mikek3332002: the executable defines an entry point. This entry point is set by the compiler at compile time. It will be set to the address of the method which you choose in project properties. .NET then calls GetCorExeMainEntrypoint() to find it. – Thomas Weller Feb 20 '14 at 11:28
  • 1
    No, you should not make `Main` public just so it can be executed from external code. That's what [Assembly.EntryPoint](http://msdn.microsoft.com/en-us/library/system.reflection.assembly.entrypoint(v=vs.110).aspx) is for. External code should not be explicitly dependent on the internal location of your entry point. If `Main` does something that you *do* expect external callers to know about, then the implementation should be moved to a dedicated public method which is called *by* `Main`. – nmclean Feb 20 '14 at 15:03
10

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

SeeMoreGain
  • 1,263
  • 16
  • 36
6

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?

Community
  • 1
  • 1
Mark Sturgill
  • 637
  • 7
  • 6
4

The main is marked as the entry point for execution in the exe itself when it is private so anything from outside cannot access it

Making it public will make the method accessible from outside

Read for more clarification http://social.msdn.microsoft.com/Forums/vstudio/en-US/9184c55b-4629-4fbf-ad77-2e96eadc4d62/why-is-main-in-c-not-a-public-static-?forum=csharpgeneral

zain27
  • 85
  • 8
3

There is difference, because first one is public and second one is private, so when you'd try to use the first one from outside the class it would work just fine, but wouldn't work with the second one.

However, there is no difference if you're trying to make one of these an entry point in you application. Entry point method can be either public or private, it doesn't matter.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
2

public and private are the access specifiers.

we use,

 public static void Main()

because to execute the program, you need to call your class in which this Main() method is present, for that you need your Main() method to be public otherwise it will not be accessible outside the class.

And the reason why it is static is, because, it needs to be accessed without creating any objects of that class .i.e. at class level.

Harish Talanki
  • 866
  • 13
  • 27
2

The private or public statement is it's access modifier, a private access modifier makes it inaccessible to external objects where a public access modifier makes it accessible to external objects. example usage:

Say we have a class:

class myClass{
    public void test(){
        //do something
    }
}

We create an instance of that class:

myClass mClass=new myClass();

To access it's member function you would go:

mClass.test();

If it had a private access modifier, you'd get a compile error saying it's inaccessible.

And just for knowledge's sake, to access a member without creating an instance of a class, you'd also make that member static, eg:

class myClass{
    public static void test(){
        //do something
    }
}

So to access it now, you'd just simply do:

myClass.test();

(Note that any members accessed in a static member should also be static)

Stuyvenstein
  • 2,340
  • 1
  • 27
  • 33
1

Based on access level.

private--> access to own class
public --> open to alll
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
1

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.