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

StackOverflow https://stackoverflow.com/questions/21871104

  •  13-10-2022
  •  | 
  •  

Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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

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

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?

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

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.

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.

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)

Based on access level.

private--> access to own class
public --> open to alll

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top