System.Collections.Immutable容器,为什么要密封? [英] System.Collections.Immutable containers, why sealed?

查看:88
本文介绍了System.Collections.Immutable容器,为什么要密封?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么System.Collections.Immutable中的容器类是不密封的,即ImmutableList<T>是密封的?

Why are the container classes in System.Collections.Immutable, ie ImmutableList<T> sealed ?

我想继承它们,并且必须经过丑陋且易于出错的组合物+代理..

I would like to inherit them and have to go through an ugly and error prone composition+proxy ..

只是想了解这里的原因?

Just trying to understand the reason here ?

推荐答案

所有类型都应密封,除非它们是为扩展而专门设计的. 设计扩展程序既困难又昂贵,而且容易出错..

All types should be sealed unless they are specifically and carefully designed for extension. Designing for extension is difficult and expensive and easy to do wrong.

此外:使用允许扩展的类型会涉及安全性和正确性.通过将类型密封,类型的作者告诉该类型的使用者如果您收到该类型的实例,则可以依靠这样的事实,即您实际上是在获取由Microsoft编写,经过Microsoft测试的类型,并具有Microsoft发布的源代码".您可以编写测试,并确信运行时行为将与测试时间行为相匹配,因为没有其他人能够进行自己的带有错误的疯狂扩展.

Moreover: there are security and correctness implications when you use a type that allows extension. By making the type sealed, the authors of the type are telling the consumers of that type "if you receive an instance of this type, you can rely on the fact that you are actually getting the type that was written by Microsoft, tested by Microsoft, and had the source code published by Microsoft". You can write tests and have confidence that the runtime behaviour will match the test time behaviour because no one else is capable of making their own crazy extension that has a bug.

问题是倒退.您永远不要问密封类型的原因. 密封应该是该语言的默认设置.相反,我们需要解开一个类型的原因:因为它是为扩展而设计的,因为它是由认真理解扩展的所有含义的专业人员实施的,并且因为该类型的消费者愿意承担不知道什么的风险他们正在调用的代码实际上是这样做的.

The question is backwards. You should never ask for a reason for a type to be sealed; sealed should have been the default in the language. Rather, we need a reason to unseal a type: because it was designed for extension, because it was implemented by professionals who carefully understood all the implications of extension, and because consumers of the type were willing to take on the risks of not knowing what the code they're calling actually does.

这篇关于System.Collections.Immutable容器,为什么要密封?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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