C# 中的内部密封类是什么?
我正在查看一些 C# 代码来扩展 VS2010 中的语言支持(Ook 示例)。我看到一些名为内部密封类
的类,
它们有什么作用?有人会使用它们吗?
I was looking through some C# code for extending language support in VS2010 (Ook example). I saw some classes called internal sealed class
What do these do? Would one use them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
它是一个具有以下特性的类:
internal
:只能从它定义的程序集(或友元程序集)内部访问。sealed
:无法继承。将类标记为内部是防止程序集的外部用户使用它们的一种方法。这实际上是一种设计封装形式,恕我直言,将不属于预期公共 API\对象模型的类型标记为
内部
是一种很好的做法。从长远来看,这可以防止您的库的用户将自己耦合到您不希望他们耦合的类型。这种意外的耦合会损害您更改和发展库实现方式的能力,因为您无法在不破坏客户端的情况下更改它们。使用internal
有助于将图书馆的公共和可用表面积保持在预期范围内。将类标记为
sealed
可防止这些类被继承。这是一个相当激烈的设计意图,如果一个类已经如此专业化,以至于不应该通过直接继承或通过覆盖其行为向其添加其他功能是明智的,那么有时会很有用。internal
和sealed
修改类型的方式截然不同,但它们可以一起使用。注意您可以对
内部
进行进一步的范围控制,因为您可以将一组其他程序集定义为“友元”。这些友元程序集可能会访问您的内部
类型。这对于定义协作组件集(例如生产和测试组件)非常有用。通常希望测试程序集能够看到它正在测试的程序集中的所有类型。It is a class that:
internal
: Can only be accessed from within the assembly it is defined (or friend assemblies).sealed
: Cannot be inherited.Marking classes as
internal
is a way of preventing outside users of an assembly from using them. It's really a form of design encapsulation and IMHO it is good practice to mark types that are not part of the intended public API\object models asinternal
. In the long term this prevents users of your library from coupling themselves to types which you did not intend them to. This sort of unintended coupling harms your ability to change and evolve the way your libraries are implemented as you cannot change them without breaking your clients. Usinginternal
helps to keep the public and usable surface area of a library down to what is intended.Marking classes as
sealed
prevents these classes from being inherited. This is a pretty drastic design intent which is sometimes useful if a class is already so specialized that it is sensible that no other functionality should be added to it via inheritance either directly or via overriding its behaviour.internal
andsealed
modify types in quite different ways, but they can be used together.NB You have some further scoping control of
internal
as you can define a set of other assemblies as 'friends'. These friend assemblies may access yourinternal
types. This can be useful for defining sets of co-operating assemblies such as production and test assemblies. It is often desirable that a test assembly can see all the types in the assembly it is testing.内部:只能在同一程序集中访问的类。
Assembly1.dll:
Assembly2.dll:
密封:无法派生的类
internal: A class which can only be accessed inside the same assembly.
Assembly1.dll:
Assembly2.dll:
sealed: A class which cannot be derived from
内部意味着同一程序集中定义的其他类型可以访问该成员。密封类有点与抽象相反。它可以被实例化,但不能作为基类。密封类的主要原因是防止用户摆弄它并破坏它。密封类还允许某些编译器优化,而这对于非密封类是不可能的。
Internal means the member is accessible to other types that are defined in the same assembly. A Sealed class is sort of the oppositie of abstract. It can be instantiated but cannot serve as a base class. The primary reason to seal a class is to prevent your users from fiddling around with it and breaking it. It’s also the case that sealing a class permits certain compiler optimizations that are not possible with non-sealed classes.
内部密封
类是这样的类:内部
- 只能从同一程序集中访问sealed
- 无法子类化换句话说,您无法直接使用它。
An
internal sealed
class is one that is:internal
- Only accessible from within the same assemblysealed
- Cannot be subclassedIn other words, there's no way for you to use it directly.
Internal 表示它只能在同一程序集中使用,
密封但不能被继承
Internal means it can be used only in same assembly,
sealed that can't be inherited
INTERNAL
内部类型或成员只能在同一程序集中的文件内访问。
示例
SEALED
首先,让我们从定义开始; seal 是一个修饰符,如果应用于类,则使其不可继承;如果应用于虚拟方法或属性,则使其不可验证。
其用法的一个示例是专用类/方法或属性,其中潜在的更改可能会使它们停止按预期工作(例如,System.Drawing 命名空间的 Pens 类)。
因为密封类不能被继承,所以不能用作基类,因此抽象类不能使用sealed 修饰符。同样重要的是要提到结构是隐式密封的。
示例
Microsoft 文档
INTERNAL
Internal types or members are accessible only within files in the same assembly.
Example
SEALED
First of all, let's start with a definition; sealed is a modifier which if applied to a class make it non-inheritable and if applied to virtual methods or properties makes them non-ovveridable.
An example of its usage is specialized class/method or property in which potential alterations can make them stop working as expected (for example, the Pens class of the System.Drawing namespace).
Because a sealed class cannot be inherited, it cannot be used as base class and by consequence, an abstract class cannot use the sealed modifier. It's also important to mention that structs are implicitly sealed.
Example
Microsoft documentation