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 HelloWorldConso leApp
{
static void Main()
{
System.Console. WriteLine("Hell o, World!");
}
}
}
(It was trivial to compile the HelloWorldConso le.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 4254
Sean Dockery <do*********@gm ail.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.co m> 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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com... Sean Dockery <do*********@gm ail.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.co m> 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*********@gm ail.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.co m> 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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com... Sean Dockery <do*********@gm ail.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.co m> 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 Explicit
Option Strict On
Imports System
|
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 object can access all the second object's data and methods
(including those that are private)'
Why?
I am not sure does that refers to:
|
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 'Main' method with an
appropriate signature was found"
Anyone have an idea what I need to do to fix this?
Thanks!
Full code follows:
| |
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 nice if there was a way for a class to create a special type of
interface, a private one. Private meaning it's not directly exposed at the
class...
|
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 server ( windows
2003 ) + IIS 6.0 , hen which giee the error =
Object reference not set to an instance of an object.This method or
property is not...
|
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 List<as a property.
Consider the example below, the class "ListTest" contains a private "List<>"
called "strings" - it also provides a public...
|
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!
Code Snippet 1:
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |