have main as private

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hello!

I just wonder why many books and some other decument on the net says that "A
main method must always be declared public and static"

I agree that main must always be static but the runtime simply ignore if you
declare main to be privat it works just as good
as if you have declared it as public.

//Tony
 
Hello,

Could be perhaps an habit carried over from other languages ? For example
java seems to make public a requirement :
http://java.sun.com/docs/books/tutorial/getStarted/application/index.html#MAIN.

The C# doc doesn't seems to tell this and would rather promote the opposite
:
"Main must be static and it should not be public".
from http://msdn.microsoft.com/en-us/library/acy3edy3.aspx

So a book author used to other languages could quite easily carry over his
knowledge to C# without noticing the difference...
 
I just wonder why many books and some other decument on the net says that "A
main method must always be declared public and static"

I agree that main must always be static but the runtime simply ignore if you
declare main to be privat it works just as good
as if you have declared it as public.

Which is very good to know if you are entering an obfuscated code
contest.

But if you plan to write easy to read code, then it makes sense
to make something that is to be called externally as public.

Arne
 
Tony said:
Hello!

I just wonder why many books and some other decument on the net says that "A
main method must always be declared public and static" [...]

Because most references fail to get every single fact they attempt to
state correct. You just can't expect to believe everything you read.

I wouldn't even hold it against them too much. That's the sort of thing
that could be easily checked, but the more things one tries to explain,
the greater the chance some mistake will slip through.

Just keep your eyes open, don't take anything as the gospel truth, and
you'll be fine.

Pete
 
Back
Top