By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,290 Members | 1,666 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Apr 14 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Sean Dockery <do*********@gmail.com> wrote:

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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 14 '06 #2

P: n/a
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.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Sean Dockery <do*********@gmail.com> wrote:

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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Apr 15 '06 #3

P: n/a
Sean Dockery <do*********@gmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 15 '06 #4

P: n/a
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.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Sean Dockery <do*********@gmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Apr 15 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.