我只想知道 private 和 protected internal 访问说明符之间的实际区别是什么。据我所知
Visible to own class members: private and protected internal YES
Visible to object of other classes: Both NO
Visible to objects of other classes outside the namespace collection: Both NO
Visible to object of child classes outside the namespace collection: Both NO
如果 private 与 protected internal 做同样的事情,那么为什么我们需要两者,只需要一个就足够了?
最佳答案
protected internal
成员对当前程序集或另一个程序集中的派生类中的任何代码可见。用技术术语来说,它是 logical disjunctionprotected
和internal
。private
成员仅对同一类中的代码可见。
protected internal
实际上是仅次于 public
的第二个最宽松的访问修饰符。
值得注意的是,protected
可以说比 internal
更宽松,因为它允许从您无法控制的代码(即其他组件)。虽然 internal
允许从当前程序集中的所有代码访问,但此代码是您的,您可以控制它!
换句话说,protected
(和protected internal
)成员是程序集公共(public) API 的一部分(因此应该记录)。 内部
成员不是。
关于c# - "private"和 "protected Internal"有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10960303/