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

What's the difference between

public static void Main() 

and

 private static void Main() 

in a C # console application? In particular, this applies to the Main() method (I understand the differences between public and private ).

+47
c # console-application
Feb 19 '14 at 4:33
source share
10 answers

To act as a starting point in an application, the Main method is not required public .

If you decide to make it public , you could call it from other classes or assemblies. Usually you do not need to do this, so you can keep it private .

One possible use case for public is to let automatic tests invoke it.

+61
Feb 19 '14 at 4:39 on
source share

The difference between the two is the only difference in the access modifiers public and private , because they are valid. It completely depends on the use of the application that you can use.

If you want to initiate an entry point with any external program (i.e. use it as an API for testing purposes), you may need to make it publicly available so that it is accessible.

public

If you know that there is no external use for the application, then it is better to make it private, so the external application will not get access to it.

private

+22
Feb 19 '14 at 9:30
source share

For most purposes this does not matter. Microsoft protects major privacy.

The only real value in this case (as far as I know) is that it will prevent the Main method from being directly called using another application code base.

A good discussion of it is available here.

+12
Feb 19 '14 at 4:40
source share

Besides the usual modifier function of open and closed access, nothing. Both are valid entry points.

See: Why is the entry point allowed as closed? and Why is the main method closed?

+7
Feb 19 '14 at 4:40
source share

The main one is marked as an entry point for execution in exe when it is private, therefore, nothing from outside can access it.

Publishing will make the method available outside

Read more clarifications 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

+5
Feb 19
source share

There is a difference, because the first of them is publicly available, and the second is private, so when you try to use the first from outside the class, it will work fine, but will not work with the second.

However, there is no difference if you are trying to make one of them an entry point into your application. The entry point method can be open or closed, it does not matter.

+4
Feb 19 '14 at 4:39 on
source share

public and private are access specifiers.

use

  public static void Main() 

because to run the program you need to call your class in which this Main () method is present, because for this you need your Main () method, which will be publicly available, otherwise it will not be available outside the class.

And the reason it is static is because it should be accessible without creating any objects of this .ie class at the class level.

+3
Feb 19 '14 at 4:43
source share

A private or public operator is an access modifier; a private access modifier makes it inaccessible to external objects, where an open access modifier makes it available to external objects. usage example:

Say we have a class:

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

Create an instance of this class:

 myClass mClass=new myClass(); 

To access this member function you have to go:

 mClass.test(); 

If he had a private access modifier, you would get a compilation error stating that this is not available.

And just for the sake of knowledge, in order to access an element without creating an instance of the class, you also need to make this element static, for example:

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

So, to access it now, just do:

 myClass.test(); 

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

+3
Feb 19 '14 at 13:35
source share

Based on access level.

 private--> access to own class public --> open to alll 
+2
Feb 19 '14 at 4:39 on
source share

For example, if you want to add an entry point that can call from outside the class or assembly, you must set public , but if it is not importatnt, use private .

+1
Feb 19 '14 at 19:39
source share



All Articles