类、方法、成员、构造函数、委托和接口的默认访问修饰符是什么?
当前回答
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.
(请注意,嵌套类型将出现在“类成员”或“结构成员”部分-因此默认为私有可见性。)
其他回答
我想添加一些文档链接。点击这里查看更多细节。
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是类的默认访问修饰符。
Private是类成员的默认访问修饰符。
类默认为内部。
默认情况下,类成员是私有的。
接口默认为“Internal”。
缺省情况下,接口成员是公共的。(接口不允许 我们不能为它的成员指定任何类型的可访问性。) 注意:如果你试图为接口的成员指定任何访问说明符,它会显示编译错误。
Struct默认为Internal。
默认情况下,结构成员是私有的。
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.
(请注意,嵌套类型将出现在“类成员”或“结构成员”部分-因此默认为私有可见性。)
推荐文章
- HTTP POST返回错误:417“期望失败。”
- 如何在。net中创建和使用资源
- 为什么Path。以Path.DirectorySeparatorChar开头的文件名合并不正确?
- 如何在c#中获得正确的时间戳
- Linq选择列表中存在的对象(A,B,C)
- c# .NET中的App.config是什么?如何使用它?
- c#:如何获得一个字符串的第一个字符?
- String类中的什么方法只返回前N个字符?
- 更好的方法将对象转换为int类型
- 我可以将c#字符串值转换为转义字符串文字吗?
- 在c#中转换char到int
- c#中朋友的对等物是什么?
- 关键字使用virtual+override vs. new
- 在ASP中选择Tag Helper。NET Core MVC
- 如何在没有任何错误或警告的情况下找到构建失败的原因