C# 中的默认访问修饰符
如果我将创建一个如下所示的新对象,默认情况下它将具有哪个访问修饰符?
Object objectA = new Object();
If I will create a new object like the following, which access modifier will it have by default?
Object objectA = new Object();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
任何成员都将始终拥有最严格的可用成员 - 因此在这种情况下,
objectA
的可访问性是private
。 (假设它是一个实例变量。它作为局部变量没有任何意义,因为它们没有任何访问规则。)因此:
相当于:
“默认为大多数私有”意味着对于类型来说,可访问性取决于上下文。这:
相当于:
... 因为你不能有一个私有的非嵌套类。
只有一个地方添加显式访问修饰符可以使某些内容比没有时更加私有,那就是在属性声明中:
Any member will always have the most restrictive one available - so in this case the accessibility of
objectA
isprivate
. (Assuming it's an instance variable. It makes no sense as a local variable, as they don't have any access rules as such.)So this:
is equivalent to this:
The "default to most private" means that for types, the accessibility depends on the context. This:
is equivalent to this:
... because you can't have a private non-nested class.
There's only one place where adding an explicit access modifier can make something more private than it is without, and that's in property declarations:
默认情况下它是私有的。
َََََ
It is private by default.
َََََ
作为类成员:
private
。如果它是在方法体内声明的局部变量,则在该方法之外无法访问它。但我猜你已经知道了。
As a class member:
private
.If it's a local variable declared within the body of a method, it has no accessibility outside that method. But I'm guessing you already knew that.
对于类成员和结构成员(包括嵌套类和结构),私有是默认值。
对于类和结构 - 内部是默认值
您可以查看 MSDN供进一步阅读..
For class members and struct members, including nested classes and structs, private is the default.
For classes and structs - internal is the default
You can check out MSDN for further reading..
类/类型本身将默认为“内部”。您创建的对象将默认为“私有”。
The class/type itself will default to "internal". The object you create will default to "private".
类和结构默认声明为内部!
了解更多信息此处
Classes and structs are declared as internal by default!
Read more here
类的访问说明符是内部的。
变量的访问说明符是私有的。
The access specifier of class is internal.
The access specifier of a variable is private.