主要的C#默认访问修饰符()方法 [英] C# Default access modifier of Main() method

查看:392
本文介绍了主要的C#默认访问修饰符()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建VS2010的样本类。

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中自动定义程序的main()方法隐私。这样做可以确保其它应用程序不能
直接调用的另一个切入点。

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?

感谢您

推荐答案

如果您出现这样的代码:

If your code appears like this:

static void Main()

那么这是一个私有方法。 (在静态部分是正交的可访问性,但要成为一个切入点。)一般情况下,任何成员的默认可访问是最私密的辅助功能,你可以声明它。因此,对于在一个类或结构的方法,这是私人的。对于顶级(非嵌套)类型的它的内部。对于一类/结构中声明任何成员,它是私有的 1 。对于接口和枚举成员,这是公开的。

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 显式接口实现是一个有点奇怪这里,因为它既不私人,也没有公开的;这根本就不是通过访问类型,只能通过该接口。

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.

这篇关于主要的C#默认访问修饰符()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆