Is there any difference if Main
method is defined in static or non-static class or is that class is public or not and if Main
method is public or not?

- 1,843
- 2
- 20
- 34
-
Any difference to _what_? – Oded Apr 08 '13 at 13:59
-
possible duplicate of [Why should main be static?](http://stackoverflow.com/questions/11332494/why-should-main-be-static) – Omar Apr 08 '13 at 14:02
5 Answers
No. The only condition is that it can't be a generic type. From section 3.1 of the C# 4 specification:
The application entry point may not be in a generic class declaration.
I suspect this was intended to also include generic struct declarations, as those would fail in the same way. (The CLR wouldn't know what type argument to provide.)
It's fine for the entry point type to be nested, static, any accessibility etc. The method itself can be private too, so long as it has an appropriate return type (void
or int
) and appropriate parameters (none or string[]
) - and it has to be static, of course.

- 1,421,763
- 867
- 9,128
- 9,194
No, that doesn't matter at all.

- 5,700
- 4
- 31
- 44
-
His question was about the `class Program` in which `Main` is defined. Also, it may be public. – pascalhein Apr 08 '13 at 14:09
According to MSDN:
Main is declared inside a class or struct. Main must be static and it should not be public. (...) The enclosing class or struct is not required to be static.
It's not entirely clear why it shouldn't be public, though. (In Java, it must be public)
The accessibility of the class/struct doesn't matter.

- 28,507
- 14
- 48
- 67
-
3I find the claim that it should not be public very odd. Nothing complains about it being public, and if you want to expose the entry point so that the same code can be called programmatically, it would make sense to *make* it public. – Jon Skeet Apr 08 '13 at 14:04
-
@Jon: probably the use of 'must' and 'should' explains that. MSDN guys are precise if not all that friendly. – dotNET Apr 08 '13 at 14:07
-
Yes, I thought that was a little strange, too.I was looking for a reason behind this but haven't found anything, yet. – Rik Apr 08 '13 at 14:07
-
This answer still doesn't answer the question as asked. You've answered one of the three questions. Does the class that the entry point is in need to be static or non-static, and does that class's accessibility matter? Also, the question didn't ask about whether the method needs to be static or not; it seems pretty clear the OP knows it must be static. – Servy Apr 08 '13 at 14:10
-
@dotNET: Not really, because there's *no* indication of why it "should" not be public. What's the benefit? – Jon Skeet Apr 08 '13 at 14:13
-
-
@Jon: Becuz generally programmers would not want the entry point of an executable to be exposed publicly. Probably executables (or just .NET executables) are not safe for **re-entry**. – dotNET Apr 08 '13 at 14:55
-
@dotNET: Whether or not it's safe to call `Main` programmatically will entirely depend on the app. It's not something which is .NET-wide. Without any justification, the advice is meaningless IMO. – Jon Skeet Apr 08 '13 at 14:58
One weird element of .NET makes it so that the safety on the Main function is actually really important.
In .NET languages, you can link to an executable as a library! That means that if, in ConsoleApplication4
, i define the full program:
namespace ConsoleApplication4
{
public class Program
{
public static int Foo(int a, int b)
{
return a + b;
}
public static void Main(string[] args)
{
}
}
}
Then I can actually make a reference in another project to ConsoleApplication4, and do this:
namespace UsedExecutableAsLibrary
{
class Program
{
static void Main(string[] args)
{
ConsoleApplication4.Program.Foo(10, 20);
ConsoleApplication4.Program.Main(args);
}
}
}
However, if I make ConsoleApplication4.Program
private, or any of the functions in Program
private, they are no longer accessible after compilation. Which behavior you want is entirely up to you, but typically you want to hide the juicy bits of your program from other people, so you make main private.

- 7,641
- 1
- 22
- 53
-
How is this disagreeing with everyone else here? Also, you haven't answered all of the three questions the OP asked. – Servy Apr 08 '13 at 14:15
-
I'll remove that since it wasn't super professional, but a bunch of people have said "it doesn't matter"...but it does! – IdeaHat Apr 08 '13 at 14:17
-
One of four people has said it doesn't matter. One said you can do either but should make it private, one said the CLR doesn't care (which is 100% correct) implying that you should use whichever accessibility is desirable for your case, rather than being forced to use one, and one person explicitly said that you should do whichever is appropriate in the context of your program. (Do you want others to be able to call that method or not?) So you're "disagreeing" with 1 out of 4 answers, and even there, not strongly. – Servy Apr 08 '13 at 14:20
-
actually I like that answer because it shows where it really may matter – user1121956 Apr 08 '13 at 14:46