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. 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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
by: Joel |
last post by:
Why does this work:
using System;
namespace ConsoleApplication1
{
class Class1
{
static void Main(string args)
|
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();}
|
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...
| |
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...
|
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...
|
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...
|
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!
...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |