473,484 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
4 4241
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3981
by: YellowDog | last post by:
I want to use a property without a private data member. This is supposed to be legal, except that I get an Stack Overflow Exception when I implicity use the set method. Here's the code: Option...
8
1403
by: Alan | last post by:
On the book C# for Experienced Programmer by Deitel, page 152, Software Engineering Observation 5.13: 'When one object of a class has a reference to another object of the same class, the first...
1
3682
by: Joel | last post by:
Why does this work: using System; namespace ConsoleApplication1 { class Class1 { static void Main(string args)
20
2377
by: Nemanja Trifunovic | last post by:
Something I don't get it: Say we have: ref class Base { virtual void SomeVirtualFunction() {Console::WriteLine(L"Base");} public: void SomeAccessibleFunction() {SomeVirtualFunction();}
9
7822
by: TechN00b | last post by:
I'm trying to write a quick commandline app that takes a string from the commandline and returns a formatted md5 hash. Unfortunately the code won't comple and returns an error of "No accessible...
6
1544
by: WXS | last post by:
I know this sounds contrary to the idea of an interface, but read this and see what you think. ----------------------------------------------------------------------------------------- It would be...
4
7174
by: ianyian | last post by:
hi experts, im doing some staff bween th the aspx + MS Word.chellcheck, and and which running on my ypc is no problem ( windows xp , activation by ASPNET ), buts someshow when i try to deploy to...
9
7850
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private...
1
2741
by: recherche | last post by:
Hola! I tried the following public implementation of interface indexer by struct (Code Snippet 1) in private and explicit implementation by struct (Code Snippet 2) but in vain. Please help! ...
0
7079
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6949
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7103
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7194
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4838
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4527
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3044
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
587
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
234
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.