什么是C#中的内部密封类? [英] What is an internal sealed class in C#?

查看:132
本文介绍了什么是C#中的内部密封类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览一些C#代码以扩展VS2010中的语言支持(好的示例).我看到了一些叫做internal sealed class

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?

推荐答案

该类是:

  • internal:只能从定义的程序集(或朋友程序集)中访问.
  • sealed:无法继承.
  • internal: Can only be accessed from within the assembly it is defined (or friend assemblies).
  • sealed: Cannot be inherited.

将类标记为internal是防止程序集的外部用户使用它们的一种方式.这实际上是设计封装的一种形式,恕我直言,将不属于预期的公共API \对象模型一部分的类型标记为internal是一种好习惯.从长远来看,这可以防止您的库用户将自己耦合到您不希望他们使用的类型.这种意外的耦合会损害您更改和发展库实现方式的能力,因为您必须在不破坏客户的情况下才能对其进行更改.使用internal有助于将库的公共和可用表面积保持在预期范围内.

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 as internal. 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. Using internal helps to keep the public and usable surface area of a library down to what is intended.

将类标记为sealed可防止这些类被继承.这是一个非常激进的设计意图,如果一个类已经非常专门化,以至于明智的做法是不应该通过继承直接或通过覆盖其行为将任何其他功能添加到该类中,则有时会很有用.

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.

internalsealed以完全不同的方式修改类型,但是它们可以一起使用.

internal and sealed modify types in quite different ways, but they can be used together.

NB 您可以对internal进行进一步的作用域控制,因为您可以将一组其他程序集定义为"friends".这些朋友程序集可以访问您的internal类型.这对于定义协作组件集(例如生产和测试组件)很有用.通常希望测试程序集可以看到正在测试的程序集中的所有类型.

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 your internal 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.

这篇关于什么是C#中的内部密封类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆