类、方法、成员、构造函数、委托和接口的默认访问修饰符是什么?
当前回答
top level class: internal
method: private
members (unless an interface or enum): private (including nested classes)
members (of interface or enum): public
constructor: private (note that if no constructor is explicitly defined, a public default constructor will be automatically defined)
delegate: internal
interface: internal
explicitly implemented interface member: public!
其他回答
类默认为内部。
默认情况下,类成员是私有的。
接口默认为“Internal”。
缺省情况下,接口成员是公共的。(接口不允许 我们不能为它的成员指定任何类型的可访问性。) 注意:如果你试图为接口的成员指定任何访问说明符,它会显示编译错误。
Struct默认为Internal。
默认情况下,结构成员是私有的。
我想添加一些文档链接。点击这里查看更多细节。
Internal是默认的修饰符
top level class: internal
method: private
members (unless an interface or enum): private (including nested classes)
members (of interface or enum): public
constructor: private (note that if no constructor is explicitly defined, a public default constructor will be automatically defined)
delegate: internal
interface: internal
explicitly implemented interface member: public!
最简单的答案是.....
c#中的所有成员默认都使用LEAST accessible修饰符。
这就是为什么程序集中的所有顶级类默认情况下都是“内部”的,这意味着它们对于所处的程序集是公共的,但对于外部程序集是私有的或排除在外的。顶级类的唯一其他选项是public,它更容易访问。对于嵌套类型,除了少数罕见的例外,如枚举成员和接口,它都是私有的,只能是公共的。一些例子。在顶级类和接口的情况下,默认值是:
class Animal与内部类Animal相同
interface动物与公共接口相同
在嵌套类和接口(内部类型)的情况下,默认值为:
class Animal与private class Animal相同
interface动物与私有接口动物相同
如果您只是假设c#类型的默认值总是最私密的,那么在需要更改默认值之前,您不需要使用自定义访问器。他们在箱子外面很安全!
推荐文章
- 实体框架核心:在上一个操作完成之前,在此上下文中开始的第二个操作
- 如何为构造函数定制Visual Studio的私有字段生成快捷方式?
- 如何使用JSON确保字符串是有效的JSON。网
- AppSettings从.config文件中获取值
- 通过HttpClient向REST API发布一个空体
- 如何检查IEnumerable是否为空或空?
- 自动化invokerrequired代码模式
- 在c#代码中设置WPF文本框的背景颜色
- 在c#中,什么是单子?
- c#和Java中的泛型有什么不同?和模板在c++ ?
- c#线程安全快速(est)计数器
- 如何将此foreach代码转换为Parallel.ForEach?
- 如何分裂()一个分隔字符串到一个列表<字符串>
- 如何转换列表<字符串>列表<int>?
- c#对象列表,我如何得到一个属性的和