How is it that a private main method is still runnable in a console application?

S

Sean Dockery

I am a Java veteran who has just started learning C# with Tom Archer's
Inside C# 2nd Ed.

In the first chapter, there is a typical Hello, World! application that
looks like this...

namespace InsideCSharp
{
class HelloWorldConsoleApp
{
static void Main()
{
System.Console.WriteLine("Hello, World!");
}
}
}

(It was trivial to compile the HelloWorldConsole.cs file with csc and run it
directly as an Windows executable.)

The chapter describes how an application must contain a single public static
Main method as an entry point into the application. It goes on to describe
how a .NET application is really MSIL embedded inside a Windows PE, where
the MSIL is compiled and executed by a JIT compiler.

From what I have read in passing so far, the default visibility of a method
in a class in C# is private. So, it seems contrary to me that the sample
code with a Main method carrying default (private) visibility can be run
from outside the class. Furthermore, I tried explicitly adding the private
modifier to the Main method signature, and it continued to run without
issue.

Can anyone help me discover where my understanding of method visibility in
C# is breaking down?

Thanks.
 
J

Jon Skeet [C# MVP]

From what I have read in passing so far, the default visibility of a method
in a class in C# is private. So, it seems contrary to me that the sample
code with a Main method carrying default (private) visibility can be run
from outside the class. Furthermore, I tried explicitly adding the private
modifier to the Main method signature, and it continued to run without
issue.

Basically, the execution of the main method is started by special code
within the CLR (or possibly code driving the CLR to start with) which
doesn't need to obey the same rules.

The same thing used to work in Java, by the way - I can't remember at
which version that changed.
 
S

Sean Dockery

Thanks for the explanation. I wonder why the author didn't emphasize that
it is a convention to make the Main method public and static--rather than a
requirement. I suppose that no tome on the subject will be perfect. :)

Thanks again.
 
J

Jon Skeet [C# MVP]

Sean Dockery said:
Thanks for the explanation. I wonder why the author didn't emphasize that
it is a convention to make the Main method public and static--rather than a
requirement. I suppose that no tome on the subject will be perfect. :)

It's absolutely necessary to make it static. It's not even a convention
particularly to make it public - I tend not to. Of course, if your Main
method is public (and the type that it's in is public) then it's easier
to run your application as part of another one, but that's rarely
useful.
 
S

Sean Dockery

Sorry, I didn't mean to imply that static was not required--just that public
was falsely specified as a requirement. Thanks for stressing that point; I
wouldn't want anyone else reading this thread to be misled.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top