C# 中默认的访问修饰符是什么?
类、方法、成员、构造函数、委托和接口的默认访问修饰符是什么?
What is the default access modifier for classes, methods, members, constructors, delegates and interfaces?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
C# 中所有内容的默认访问权限是“您可以为该成员声明的最受限制的访问权限”。
例如:
相当于
一种例外是使属性的一部分(通常是 setter)比属性本身声明的可访问性更受限制:
这就是 C# 3.0 规范必须说的(第 3.5 节) .1):
(请注意,嵌套类型将位于“类成员”或“结构成员”部分下 - 因此默认为私有可见性。)
The default access for everything in C# is "the most restricted access you could declare for that member".
So for example:
is equivalent to
The one sort of exception to this is making one part of a property (usually the setter) more restricted than the declared accessibility of the property itself:
This is what the C# 3.0 specification has to say (section 3.5.1):
(Note that nested types would come under the "class members" or "struct members" parts - and therefore default to private visibility.)
简短回答:最小可能的访问权限(参见 Jon Skeet 的回答)。
长答案:
非嵌套类型、枚举和委托可访问性(可能仅具有内部或公共可访问性)
嵌套类型和成员可访问性
嵌套类型的可访问性取决于其可访问域,这是由成员声明的可访问性和直接包含类型的可访问性域决定的。但是,嵌套类型的可访问域不能超过包含类型的可访问域。
注意:CIL 还提供了受保护和内部的规定(与现有的受保护的“或”内部相反),但据我所知,目前在 C# 中无法使用。
请参阅:
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
(我喜欢微软的 URL...)
Short answer: minimum possible access (cf Jon Skeet's answer).
Long answer:
Non-nested types, enumeration and delegate accessibilities (may only have internal or public accessibility)
Nested type and member accessiblities
The accessibility of a nested type depends on its accessibility domain, which is determined by both the declared accessibility of the member and the accessibility domain of the immediately containing type. However, the accessibility domain of a nested type cannot exceed that of the containing type.
Note: CIL also has the provision for protected and internal (as opposed to the existing protected "or" internal), but to my knowledge this is not currently available for use in 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
(Man I love Microsoft URLs...)
看看 访问修饰符(C# 编程指南)
Have a look at Access Modifiers (C# Programming Guide)
默认情况下,类是内部。
默认情况下,接口是内部。
默认情况下,接口成员是公开的。 (接口不允许
我们指定其成员的任何类型的可访问性。)
注意:如果您尝试为接口的成员指定任何访问说明符,则会显示编译错误。
默认情况下,结构是内部。
Class is Internal by default.
Interface is Internal by default.
Interface members are public by default. (Interfaces won't allow
us to specify any kind of accessibility to it's members.)
Note: If you try to specify any access specifier to interface's members then, it shows compile error.
Struct is Internal by default.
我想添加一些文档链接。请在此处查看更多详细信息。
I would like to add some documentation link. Check out more detail here.
最简单的答案如下...
默认情况下,C# 中的所有成员始终采用可能的 LEAST 可访问修饰符。
这就是为什么程序集中的所有顶级类默认都是“内部”的,这意味着它们对其所在的程序集是公开的,但对外部程序集是私有的或被排除在外。顶级类的唯一其他选择是公共的,它更容易访问。对于嵌套类型,除了一些罕见的例外(例如只能是公共的枚举和接口的成员)之外,它都是私有的。一些例子。
中有两类访问修饰符,
在 C#顶级...
和成员级(在另一个类中等)
每种访问修饰符都有自己不同的默认值。一般来说,所有顶层都可以是
internal
(默认,与无访问修饰符相同,并且阻止外部程序集访问)或public
(对程序集和外部程序集访问开放) 。顶级类型访问修饰符
对于顶级类、结构、接口等,默认情况下都是
内部
,如使用“animal”类所示举个例子。这意味着它们默认不对外部程序集访问:class Animal{} 与
internal
class Animal{}< /strong>interface Animal{} 与
internal
interface Animal{}成员级别类型访问修饰符
对于嵌套类和接口(内部类型),默认值通常是
private
,除了接口和枚举是public
之外。这通常意味着类中的成员无法从内部程序集及其内部和外部程序集对类成员进行外部访问:类 Animal{} 与
private
class Animal{} 相同interface Animal{} 与
public
接口 Animal{}了解您的顶级类型还控制它们内部的成员。因此,如果将顶级访问修饰符从
internal
更改为public
,则可以突然访问类内的任何公共成员或受保护的派生成员由外部集会!请记住,当从控制可访问性的基类派生时,这些规则也可能被打破。但是,如果您只是假设 C# 类型的默认值始终是最私有的,那么在需要更改默认值之前,您不需要使用自定义访问修饰符。它们开箱即用,非常安全!
Simplest answer is the following.....
All members in C# always take the LEAST accessible modifier possible by default.
That is why all top level classes in an assembly are "internal" by default, which means they are public to the assembly they are in, but private or excluded from access to outside assemblies. The only other option for a top level class is public which is more accessible. For nested types its all private except for a few rare exceptions like members of enums and interfaces which can only be public. Some examples.
There are TWO CATEGORIES OF ACCESS MODIFIERS in C#
Top-Level...
...and Member-Level (inside another class, etc)
Each has their own defaults that are different. In general all top level can be
internal
(the default, same as no access modifier, and prevented from outside assembly access) orpublic
(open to assembly and outside assembly access).TOP-LEVEL TYPE ACCESS MODIFIERS
In the case of top level classes, structs, interfaces, etc. the defaults are all
internal
by default, as seen using the "animal" class as an example. This means they are closed to external assembly access by default:class Animal{} is the same as
internal
class Animal{}interface Animal{} is the same as
internal
interface Animal{}MEMBER-LEVEL TYPE ACCESS MODIFIERS
In the case of nested classes and interfaces (inside types), the defaults are usually
private
except for interfaces and enums which arepublic
. This generally means members in classes are excluded from both outside access to the class members from both the internal assembly its in and outside assemblies:class Animal{} is the same as
private
class Animal{}interface Animal{} is the same as
public
interface Animal{}Understand that your top-level types also control the members inside them. So if you change a top-level access modifier from
internal
topublic
, any public members or protected derived ones inside the class can suddenly be accessed by outside assemblies!Keep in mind these rules can be broken when deriving from base classes which control accessibility, too. But if you just assume the default on C# types is always the most private, then you do not need to use custom access modifiers until you need to change the default. They are secure out of the box!
命名空间级别:
internal
类型级别:
private
Namespace level:
internal
Type level:
private
internal
是类的默认访问修饰符。private
是类成员的默认访问修饰符。internal
is default access modifier for class.private
is default access modifier for class members.Internal 是默认修饰符
Internal is the default modifier