C# Default access modifier of Main() method

2020-03-14 12:30发布

I create a sample class in vs2010.

Through Class View, I see the default access modifier for Main is internal.

I also see some people say that the default access modifier for Main is "implicitly private".

Visual Studio 2010 automatically defines a program’s Main() method as implicitly private. Doing so ensures other applications cannot directly invoke the entry point of another.

I know there are differences between internal and private. So which one is correct?

标签: c#
6条回答
淡お忘
2楼-- · 2020-03-14 13:12

If your code appears like this:

static void Main()

then that's a private method. (The static part is orthogonal to accessibility, but is necessary to be an entry point.) In general, the default accessibility of any member is the most private accessibility that you could declare it. So for methods in a class or a struct, that's private. For top-level (non-nested) types it's internal. For any member declared in a class/struct, it's private1. For interface and enum members, it's public.

It's hard to understand exactly what you're seeing via Class View without seeing either your code or a screenshot of Class View, but the default accessibility for a method is definitely private. That's true regardless of whether it's the Main method or not.


1 Explicit interface implementation is a bit odd here, as it's neither private nor public; it's simply not accessible through the type, only through the interface.

查看更多
劳资没心,怎么记你
3楼-- · 2020-03-14 13:16

You can't see the default access modifier for a member in the class browser, you can see the actual access modifier.

The default access modifiers for classes at the namespace level is internal, whereas the default access modifier for class members (including nested classes) is private. There's no special case for the Main() function. If there's no access modifier before it (a la Jon Skeet's example), then it's private. If there is one, then that's what it is.

查看更多
家丑人穷心不美
4楼-- · 2020-03-14 13:16

By Default the access specifier for Main() in C# is private.

This is what I got when I saw the MSIL(IL) code in ILDASM.

You can see that Main() is private. IL code of Simple Program in C#

查看更多
Summer. ? 凉城
5楼-- · 2020-03-14 13:23

Both, the default class modifier is internal. The main method is a method and is private. In general, classes without a modifier are internal, class-members (such as methods and fields) without a declaration are private.

查看更多
在下西门庆
6楼-- · 2020-03-14 13:27

Private members are accessible only within the body of the class in which they are declared.

Internal types or members are accessible only within files in the same assembly

Internal 'is like' public but only for all elements of the same assembly. Class1 of assembly1 cannot 'see' or access any internal element of assembly2.

查看更多
干净又极端
7楼-- · 2020-03-14 13:37

Although you tagged your question , let me say that the access modifiers for the default Program.Main generated by VS2010 actually depends on the project template, on these differ for each language. I quickly tried the following:

  • In a VB.NET console project, the Program module (static class) is Friend (i.e. internal in C#) and the Main static method is Public.

  • In a C# console project, Program is internal, and Main is private.

That is, a C# project will simply use the default access modifiers (internal for classes, private for methods).

查看更多
登录 后发表回答