为什么&QUOT的概念,协方差"和"逆变"适用而实现接口的方法呢? [英] Why the concept of "Covariance" and "Contravariance" are applicable while implementing the methods of an interface?

查看:141
本文介绍了为什么&QUOT的概念,协方差"和"逆变"适用而实现接口的方法呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该用例是一些什么样的:

The use case is some what like this:

public class SomeClass: IClonable
 {
     // Some Code

     //Implementing interface method
     Public object Clone()
      {
        //Some Clonning Code
      }
 }

现在我的问题是:为什么不能使用SomeClass的(因为它是从objec derivd)的clone()方法的返回类型,如果我们考虑到丰达的协变和逆变的。

Now my question is "Why is it not possible to use "SomeClass(As it is derivd from objec)" as a return type of Clone() method if we consider the Funda's of Covariance and Contravariance.

有人可以解释我的原因,这implemementaion微软背后????

Can somebody explain me the reason behind this implemementaion of Microsoft ????

推荐答案

一个非破碎执行接口实现差异就已经在返回类型,协变的和逆变的参数类型的。

A non-broken implementation of interface-implementation variance would have to be covariant in the return type and contravariant in the argument types.

例如:

public interface IFoo
{
    object Flurp(Array array);
}

public class GoodFoo : IFoo
{
    public int Flurp(Array array) { ... }
}

public class NiceFoo : IFoo
{
    public object Flurp(IEnumerable enumerable) { ... }
}

两者都是在新规则的法律,对不对?但对于这样的:

Both are legal under the "new" rules, right? But what about this:

public class QuestionableFoo : IFoo
{
    public double Flurp(Array array) { ... }
    public object Flurp(IEnumerable enumerable) { ... }
}

一种很难判断哪些隐含的实现是更好地在这里。第一个是参数类型的返回类型完全匹配,但不是。二是返回类型参数类型的精确匹配,但不是。我倾向于第一个,因为谁使用了的IFoo 接口,永远只能给它一个阵列,但它的仍不完全清楚。

Kind of hard to tell which implicit implementation is better here. The first one is an exact match for the argument type, but not the return type. The second is an exact match for the return type, but not the argument type. I'm leaning toward the first, because whoever uses the IFoo interface can only ever give it an Array, but it's still not entirely clear.

这还不是最坏的,到目前为止。如果我们这样做,而不是:

And this isn't the worst, by far. What if we do this instead:

public class EvilFoo : IFoo
{
    public object Flurp(ICollection collection) { ... }
    public object Flurp(ICloneable cloneable) { ... }
}

哪一个赢得奖金?这是一个完全有效的过载,但的ICollection ICloneable 什么都没有做,彼此数组同时实现了他们。我看不到一个明显的解决方案在这里。

Which one wins the prize? It's a perfectly valid overload, but ICollection and ICloneable have nothing to do with each other and Array implements both of them. I can't see an obvious solution here.

它只会变得更糟,如果我们开始添加过载的接口本身:

It only gets worse if we start adding overloads to the interface itself:

public interface ISuck
{
    Stream Munge(ArrayList list);
    Stream Munge(Hashtable ht);
    string Munge(NameValueCollection nvc);
    object Munge(IEnumerable enumerable);
}

public class HateHateHate : ISuck
{
    public FileStream Munge(ICollection collection);
    public NetworkStream Munge(IEnumerable enumerable);
    public MemoryStream Munge(Hashtable ht);
    public Stream Munge(ICloneable cloneable);
    public object Munge(object o);
    public Stream Munge(IDictionary dic);
}

祝你好运试图解开这个谜团没有要疯了。

Good luck trying to unravel this mystery without going insane.

当然,这一切是没有实际意义,如果你断言接口实现应该只支持返回类型差异而不是参数型变异。但是,几乎每个人都会考虑这样的半实现被完全打破,并开始发送垃圾邮件的错误报告,所以我不认为C#团队要做到这一点。

Of course, all of this is moot if you assert that interface implementations should only support return-type variance and not argument-type variance. But almost everyone would consider such a half-implementation to be completely broken and start spamming bug reports, so I don't think that the C# team is going to do it.

我不知道这是为什么它在C#中的今天,不支持官方的理由,但它应作为一种很好的例子只写code,它可能会导致,和的C#团队的设计理念的一部分就是尽量prevent开发人员编写可怕code。

I don't know if this is the official reason why it's not supported in C# today, but it should serve as a good example of the kind of "write-only" code that it could lead to, and part of the C# team's design philosophy is to try to prevent developers from writing awful code.

这篇关于为什么&QUOT的概念,协方差"和"逆变"适用而实现接口的方法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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