为什么C#(4.0)不允许在泛型类类型合作和逆变? [英] Why does C# (4.0) not allow co- and contravariance in generic class types?

查看:275
本文介绍了为什么C#(4.0)不允许在泛型类类型合作和逆变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是在真正原因是什么限制?难道仅仅是必须做的工作?它是在概念上很难吗?它是不可能的?

What is the real reason for that limitation? Is it just work that had to be done? Is it conceptually hard? Is it impossible?

当然,一个人不能在字段中使用类型参数,因为它们是百达读写。但是,这不能成为答案,可以吗?

Sure, one couldn't use the type parameters in fields, because they are allways read-write. But that can't be the answer, can it?

这样做的原因的问题是,我写在C#4方差支持的文章,我觉得我应该解释为什么它仅限于代表和接口。只是逆举证责任。

The reason for this question is that I'm writing an article on variance support in C# 4, and I feel that I should explain why it is restricted to delegates and interfaces. Just to inverse the onus of proof.

更新:
埃里克问一个例子。

Update: Eric asked about an example.

这个是什么(不知道如果是有道理的,但: - ))

What about this (don't know if that makes sense, yet :-))

public class Lookup<out T> where T : Animal {
  public T Find(string name) {
    Animal a = _cache.FindAnimalByName(name);
    return a as T;
  }
}

var findReptiles = new Lookup<Reptile>();
Lookup<Animal> findAnimals = findReptiles;

原因具有一类可能是在类本身持有的缓存。请不要命名您的不同类型的宠物一样!

The reason for having that in one class could be the cache that is held in the class itself. And please don't name your different type pets the same!

顺便说一句,这使我想起<一个href=\"http://stackoverflow.com/questions/2541659/what-about-optional-generic-type-parameters-in-c-5-0\">optional在C#中的类型参数5.0 : - )

BTW, this brings me to optional type parameters in C# 5.0 :-)

更新2:我不是说了CLR和C#应该允许这一点。只是想了解是什么导致了它犯规。

Update 2: I'm not claiming the CLR and C# should allow this. Just trying to understand what led to that it doesnt.

推荐答案

首先,作为托马斯说,它不是在CLR支持

First off, as Tomas says, it is not supported in the CLR.

二,如何将这项工作?假设你有

Second, how would that work? Suppose you have

class C<out T>
{ ... how are you planning on using T in here? ... }

T能够只在输出位置中使用。当你注意,类不能有类型T的任何领域,因为该领域可能会被写入。类不能有拿一件T的任何方法,因为那些在逻辑上写道。假设你有这个功能 - 你会如何利用它的优势

T can only be used in output positions. As you note, the class cannot have any field of type T because the field could be written to. The class cannot have any methods that take a T, because those are logically writes. Suppose you had this feature -- how would you take advantage of it?

这将是不可改变的类,如果有可能,说有用,使其合法拥有类型T的一个只读场;这样,我们会大量减少,这不正确写入的可能性。但是,这是相当困难拿出允许在方差类型安全的方式其他场景。

This would be useful for immutable classes if we could, say, make it legal to have a readonly field of type T; that way we'd massively cut down on the likelihood that it be improperly written to. But it's quite difficult to come up with other scenarios that permit variance in a typesafe manner.

如果你有这样的情况,我很乐意看到它。这将是对分获得某一天在CLR这个实现的。

If you have such a scenario, I'd love to see it. That would be points towards someday getting this implemented in the CLR.

更新:请参见

<一个href=\"http://stackoverflow.com/questions/2733346/why-isnt-there-generic-variance-for-classes-in-c-4-0/2734070#2734070\">http://stackoverflow.com/questions/2733346/why-isnt-there-generic-variance-for-classes-in-c-4-0/2734070#2734070

更多关于这个问题的。

这篇关于为什么C#(4.0)不允许在泛型类类型合作和逆变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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