简体   繁体   English

如果我在 C# 中的 Main 方法是私有的,为什么我的程序可以工作?

[英]Why does my program work if my Main method in C# is private?

By default the type modifier for every member in a class is a private, even the Main() function type modifier is private.默认情况下,class 中每个成员的类型修饰符都是私有的,即使 Main() function 类型修饰符也是私有的。 How does the CLR call the main method which is not visible to the outside world? CLR如何调用外界不可见的main方法?

The CLR does not care about the accessibility of main . CLR 不关心main的可访问性。 "Visible to the outside world" only applies to the code , not the runtime . “对外界可见”仅适用于代码,不适用于运行时

Thats not true. 这不是真的。

It has to be public. 它必须是公开的。 For eg public static void Main() . 例如, public static void Main()

EDIT: Here is what I found & learned today, on why Main need not be public .编辑:这是我今天发现和学到的,关于为什么Main不需要public http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/9184c55b-4629-4fbf-ad77-2e96eadc4d62/ http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/9184c55b-4629-4fbf-ad77-2e96eadc4d62/

Try using ildasm on your code and lookout for the main method尝试在您的代码上使用ildasm并寻找主要方法

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint // this is something the CLR is interested in

You're right,你是对的,

it's marked as an entrypoint.它被标记为入口点。 Check this question: Why is Main method private?检查这个问题:为什么 Main 方法是私有的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM