类、方法、成员、构造函数、委托和接口的默认访问修饰符是什么?
当前回答
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!
其他回答
我想添加一些文档链接。点击这里查看更多细节。
简而言之:尽可能少的访问(参见Jon Skeet的回答)。
长一点的回答:
非嵌套类型、枚举和委托可访问性(可能只有内部或公共可访问性)
|默认|允许声明的访问权限 ------------------------------------------------------------------ 命名空间| public | none(总是隐式公共) Enum | public | public,内部 接口| internal | public, internal 类| internal | public, internal Struct | internal | public, internal 委托|内部|公共,内部
嵌套类型和成员可访问性
| Default | Permitted declared accessibilities ------------------------------------------------------------------ namespace | public | none (always implicitly public) enum | public | All¹ interface | public | All¹ class | private | All¹ struct | private | public, internal, private² delegate | private | All¹ constructor | private | All¹ enum member | public | none (always implicitly public) interface member | public | none (always implicitly public) method | private | All¹ field | private | All¹ user-defined operator| none | public (must be declared public) ¹ All === public, protected, internal, private, protected internal ² structs cannot inherit from structs or classes (although they can, interfaces), hence protected is not a valid modifier
嵌套类型的可访问性取决于其可访问域,该域由成员声明的可访问性和直接包含类型的可访问性域共同决定。但是,嵌套类型的可访问域不能超过包含类型的可访问域。
注意:CIL还提供了受保护的和内部的(与现有的受保护的”或“内部的”相对),但据我所知,这在c#中目前是不可用的。
See:
http://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx http://msdn.microsoft.com/en-us/library/ms173121.aspx http://msdn.microsoft.com/en-us/library/cx03xt0t.aspx (我喜欢微软的网址…)
看一下Access Modifiers (c#编程指南)
Class and Struct Accessibility Classes and structs that are declared directly within a namespace (in other words, that are not nested within other classes or structs) can be either public or internal. Internal is the default if no access modifier is specified. Struct members, including nested classes and structs, can be declared as public, internal, or private. Class members, including nested classes and structs, can be public, protected internal, protected, internal, private protected or private. The access level for class members and struct members, including nested classes and structs, is private by default. Private nested types are not accessible from outside the containing type. Derived classes cannot have greater accessibility than their base types. In other words, you cannot have a public class B that derives from an internal class A. If this were allowed, it would have the effect of making A public, because all protected or internal members of A are accessible from the derived class. You can enable specific other assemblies to access your internal types by using the InternalsVisibleToAttribute. For more information, see Friend Assemblies. Class and Struct Member Accessibility Class members (including nested classes and structs) can be declared with any of the six types of access. Struct members cannot be declared as protected because structs do not support inheritance. Normally, the accessibility of a member is not greater than the accessibility of the type that contains it. However, a public member of an internal class might be accessible from outside the assembly if the member implements interface methods or overrides virtual methods that are defined in a public base class. The type of any member that is a field, property, or event must be at least as accessible as the member itself. Similarly, the return type and the parameter types of any member that is a method, indexer, or delegate must be at least as accessible as the member itself. For example, you cannot have a public method M that returns a class C unless C is also public. Likewise, you cannot have a protected property of type A if A is declared as private. User-defined operators must always be declared as public and static. For more information, see Operator overloading. Finalizers cannot have accessibility modifiers. Other Types Interfaces declared directly within a namespace can be declared as public or internal and, just like classes and structs, interfaces default to internal access. Interface members are always public because the purpose of an interface is to enable other types to access a class or struct. No access modifiers can be applied to interface members. Enumeration members are always public, and no access modifiers can be applied. Delegates behave like classes and structs. By default, they have internal access when declared directly within a namespace, and private access when nested.
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#类型的默认值总是最私密的,那么在需要更改默认值之前,您不需要使用自定义访问器。他们在箱子外面很安全!
推荐文章
- 为什么Func<T,bool>而不是Predicate<T>?
- .NET中的Map和Reduce
- 我如何能使一个组合框不可编辑的。net ?
- .NET反射的成本有多高?
- 实体框架回滚并移除不良迁移
- 将流转换为字符串并返回
- 在c#中检查字符串是否只包含数字的最快方法
- IEquatable和重写Object.Equals()之间的区别是什么?
- 创建一个堆栈大小为默认值50倍的线程有什么危险?
- 转换JSON字符串到JSON对象c#
- 显示两个datetime值之间的小时差值
- 如何设置enum为空
- 选择Enum类型的默认值而无需更改值
- 我如何设置在一个组合框中选择的项目,以匹配我的字符串使用c# ?
- String与StringBuilder