当前位置:编程学习 > C#/ASP.NET >>

为什么Main方法不应为Public?

MSDN有言:
Main方法不应为公共方法。
这是为什么?我试了,是可以声明为Public的,只是不知道为什么不应为Public呢? --------------------编程问答-------------------- 因为一个 C# 程序中只能有一个入口点。 --------------------编程问答--------------------
引用 1 楼 q107770540 的回复:
因为一个 C# 程序中只能有一个入口点。


我想知道,这个“公有”与“私有”是相对于谁而言的? --------------------编程问答-------------------- MSDN写的should not be public,但是没有写为什么,我觉得Main特殊的函数,无论如何编译器都会认识它的,因此无所谓public或者Private

    *

      The Main method is the entry point of your program, where the program control starts and ends.
    *

      It is declared inside a class or struct. It must be static and it should not be public. (In the example above it receives the default access of private.)
    *

      It can either have a void or int return type.
    *

      The Main method can be declared with or without parameters.
    *

      Parameters can be read as zero-indexed command line arguments.
    *

      Unlike C and C++, the name of the program is not treated as the first command line argument.

参考:
http://msdn.microsoft.com/en-us/library/acy3edy3%28VS.80%29.aspx --------------------编程问答-------------------- MSDN写的should not be public,但是没有写为什么,我觉得Main特殊的函数,无论如何编译器都会认识它的,因此无所谓public或者Private

    *

      The Main method is the entry point of your program, where the program control starts and ends.
    *

      It is declared inside a class or struct. It must be static and it should not be public. (In the example above it receives the default access of private.)
    *

      It can either have a void or int return type.
    *

      The Main method can be declared with or without parameters.
    *

      Parameters can be read as zero-indexed command line arguments.
    *

      Unlike C and C++, the name of the program is not treated as the first command line argument.

参考:
http://msdn.microsoft.com/en-us/library/acy3edy3%28VS.80%29.aspx --------------------编程问答-------------------- --------------------编程问答-------------------- up++ --------------------编程问答-------------------- 瞎猜:也许是怕被其他代码调用,反正编译器认识,不如弄成private安全点 --------------------编程问答-------------------- Java是建议你声明Main函数为Public,所以别太纠结于此点

--------------------编程问答-------------------- Main是大门,每家每户的大门都应该是私有的吧? --------------------编程问答-------------------- 应该深入研究一下。 --------------------编程问答-------------------- 可以随便调用的话,估计会引起程序崩溃。。 --------------------编程问答-------------------- 求微软专家解答,并此对事作出回应 --------------------编程问答-------------------- --------------------编程问答-------------------- 一个程序一个mian方法,发现过问题,没研究过。。。 --------------------编程问答-------------------- --------------------编程问答-------------------- 额....还有没回答啊,这怎么好结贴呢,这可是我发的第一个帖子..... --------------------编程问答-------------------- 没有定义.有主函数就可以了 --------------------编程问答--------------------
引用 7 楼 phil999 的回复:
瞎猜:也许是怕被其他代码调用,反正编译器认识,不如弄成private安全点


同意。

我猜msdn上这个描述是来自于许多年前,而且是个“权威”写的,所以也许之后的msdn维护人员备份太低没有人敢(或者没有人有那个闲心)去修改它。 --------------------编程问答--------------------
引用 11 楼 gene_x 的回复:
可以随便调用的话,估计会引起程序崩溃。。


没有这个必然性。

任何public方法都会经历测试,是否会引起程序崩溃这要由测试说了算。而如果我们写一个什么都不做的空的Main函数并不会让程序崩溃,所以说它就会引擎程序崩溃显然是不成立的(不是真正决定是否崩溃的原因)。

同样道理,如果lz测试出来Main声明为public也一样,那么msdn上的措辞显然就经不起测试,就是一个文档方面的bug。 --------------------编程问答-------------------- MAIN为PUBLIC有没有可能导致程序走迷宫?可能还是为了不被其它任何地方调用才有这样的建议吧。楼主在多个地方调用下MAIN函数看看有没有问题啥
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,