类、方法、成员、构造函数、委托和接口的默认访问修饰符是什么?
当前回答
Internal是类的默认访问修饰符。
Private是类成员的默认访问修饰符。
其他回答
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!
看一下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.
命名空间级别:internal
类型级别:private
c#中所有东西的默认访问权限是“你可以为该成员声明的最受限制的访问权限”。
例如:
namespace MyCompany
{
class Outer
{
void Foo() {}
class Inner {}
}
}
等于
namespace MyCompany
{
internal class Outer
{
private void Foo() {}
private class Inner {}
}
}
这种情况的一个例外是使属性的一部分(通常是setter)比属性本身声明的可访问性更受限制:
public string Name
{
get { ... }
private set { ... } // This isn't the default, have to do it explicitly
}
这是c# 3.0规范必须说的(第3.5.1节):
Depending on the context in which a member declaration takes place, only certain types of declared accessibility are permitted. Furthermore, when a member declaration does not include any access modifiers, the context in which the declaration takes place determines the default declared accessibility. Namespaces implicitly have public declared accessibility. No access modifiers are allowed on namespace declarations. Types declared in compilation units or namespaces can have public or internal declared accessibility and default to internal declared accessibility. Class members can have any of the five kinds of declared accessibility and default to private declared accessibility. (Note that a type declared as a member of a class can have any of the five kinds of declared accessibility, whereas a type declared as a member of a namespace can have only public or internal declared accessibility.) Struct members can have public, internal, or private declared accessibility and default to private declared accessibility because structs are implicitly sealed. Struct members introduced in a struct (that is, not inherited by that struct) cannot have protected or protected internal declared accessibility. (Note that a type declared as a member of a struct can have public, internal, or private declared accessibility, whereas a type declared as a member of a namespace can have only public or internal declared accessibility.) Interface members implicitly have public declared accessibility. No access modifiers are allowed on interface member declarations. Enumeration members implicitly have public declared accessibility. No access modifiers are allowed on enumeration member declarations.
(请注意,嵌套类型将出现在“类成员”或“结构成员”部分-因此默认为私有可见性。)
类默认为内部。
默认情况下,类成员是私有的。
接口默认为“Internal”。
缺省情况下,接口成员是公共的。(接口不允许 我们不能为它的成员指定任何类型的可访问性。) 注意:如果你试图为接口的成员指定任何访问说明符,它会显示编译错误。
Struct默认为Internal。
默认情况下,结构成员是私有的。