为什么不能C#泛型从泛型类型参数之一派生像他们可以在C ++模板? [英] Why cannot C# generics derive from one of the generic type parameters like they can in C++ templates?

查看:1105
本文介绍了为什么不能C#泛型从泛型类型参数之一派生像他们可以在C ++模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不能C#泛型从泛型类型参数之一派生像他们可以在C ++模板?我的意思是我知道这是不可能的,因为CLR不支持这一点,但为什么?

Why cannot C# generics derive from one of the generic type parameters like they can in C++ templates? I mean I know it impossible because CLR does not support this, but why?

我知道C ++模板和C#泛型之间的深刻分歧 - 前者是编译时的实体,必须在编译过程中得到解决,而后者则是一流的运行时间实体

I am aware of the profound differences between C++ templates and C# generics - the former are compile time entities and must be resolved during the compilation, while the latter are first class run-time entities.

不过,我没有看到的的原因为什么CLR的设计者并没有拿出一项计划,将最终使CLR泛型类型从它的泛型类型参数之一派生。毕竟,这将是相当有用的功能,我个人非常怀念它。

Still, I am failing to see the reason why CLR designers did not come up with a scheme which would ultimately enable a CLR generic type to derive from one of its generic type parameters. After all, this would be tremendously useful feature, I personally miss it greatly.

编辑:

我想知道一个硬核问题,固定它产生这么高的价格上实现这一功能,证明它没有被实施的尚。例如,检查这个虚构的声明:

I would like to know of a hard-core issue, fixing which yields such a high price on implementing this feature that justifies it not being implemented yet. For instance, examine this fictional declaration:

class C<T> : T
{
}

由于埃里克利珀已经注意到,如果如果T是一个结构呢?如果T是一个密封类类型呢?如果T是一个接口类型是什么?如果T是C?!如果T是类从C dervied?如果T是一个抽象类型与抽象方法是什么?如果T比C无障碍少了呢?如果T是什么System.ValueType?(你能有一个非结构继承自System.ValueType?)那么System.Delegate,System.Enum,等等?

随着埃里克继续,这些都是简单的,显而易见的。事实上,他是对的。我感兴趣的是一些既不容易,也没有明显的问题,这是很难解决一个具体的例子。

As Eric continues, "Those are the easy, obvious ones". Indeed, he is right. I am interested in a concrete example of some neither easy nor obvious issue, which is hard to resolve.

推荐答案

好了,通过问自己就什么可能去错了类℃下T&GT; :T已{} 。一个事物的数量庞大立刻来在脑海中:

Well, start by asking yourself what could possibly go wrong with class C<T> : T { }. A huge number of things come immediately to mind:

如果T是一个结构?如果T是一个密封类类型?如果T是什么接口类型?如果T是 C&LT; T&GT; ?如果T是从派生的类C&LT; T&GT; ?如果T是什么抽象类型与抽象的方法?如果T有比C无障碍少?如果T是System.ValueType? (你可以有一个非结构继承自System.ValueType?)怎么样System.Delegate,System.Enum,等等?

What if T is a struct? What if T is a sealed class type? What if T is an interface type? What if T is C<T>?! What if T is a class derived from C<T>? What if T is an abstract type with an abstract method? What if T has less accessibility than C ? What if T is System.ValueType? (Can you have a non-struct which inherits from System.ValueType?) What about System.Delegate, System.Enum, and so on?

这些都是简单的,明显的。所提出的特征开辟了数以百计,甚至数千左右的类型和它的基类型,所有这些都必须小心地指定,实施和测试之间的相互作用更微妙的问题。我们无疑会错过一些,从而导致在未来的重大更改,或鞍实现定义的运行时间。

Those are the easy, obvious ones. The proposed feature opens up literally hundreds, if not thousands of more subtle questions about the interaction between the type and its base type, all of which would have to be carefully specified, implemented and tested. We'd undoubtedly miss some, and thereby cause breaking changes in the future, or saddle the runtime with implementation-defined behaviour.

该费用将是巨大的,所以效益最好是巨大的。我没有看到一个巨大的利益在这里。

The costs would be enormous, so the benefit had better be enormous. I'm not seeing an enormous benefit here.

这篇关于为什么不能C#泛型从泛型类型参数之一派生像他们可以在C ++模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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