"接口没有实现"返回派生类型时 [英] "Interface not implemented" when Returning Derived Type

查看:214
本文介绍了"接口没有实现"返回派生类型时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下code:

public interface ISomeData
{
    IEnumerable<string> Data { get; }
}

public class MyData : ISomeData
{
    private List<string> m_MyData = new List<string>();
    public List<string> Data { get { return m_MyData; } }
}

产生以下错误:

错误CS0738:'InheritanceTest.MyData
  未实现接口成员
  InheritanceTest.ISomeData.Data。
  InheritanceTest.MyData.Data不能
  实行
  InheritanceTest.ISomeData.Data
  因为它不具有匹配
  返回类型
  System.Collections.Generic.IEnumerable。

error CS0738: 'InheritanceTest.MyData' does not implement interface member 'InheritanceTest.ISomeData.Data'. 'InheritanceTest.MyData.Data' cannot implement 'InheritanceTest.ISomeData.Data' because it does not have the matching return type of 'System.Collections.Generic.IEnumerable'.

由于一个List&LT; T&GT;实现IEnumerable&LT; T&GT ;,人们可能会认为我的课会实现的接口。有人可以解释的理由是什么,这不是编译?

Since a List<T> implements IEnumerable<T>, one would think that my class would implement the interface. Can someone explain what the rationale is for this not compiling?

据我可以看到它,有两种可能的解决方案:

As I can see it, there are two possible solutions:


  1. 更改界面更具体,要求IList的实现。

  2. 改变我的课(迈德特)返回IEnumerable和执行原来的界面。

现在假设我也有以下的code:

Now suppose I also have the following code:

public class ConsumerA
{
    static void IterateOverCollection(ISomeData data)
    {
        foreach (string prop in data.MyData)
        {
            /*do stuff*/
        }
    }
}

public class ConsumerB
{
    static void RandomAccess(MyData data)
    {

        data.Data[1] = "this line is invalid if MyPropList return an IEnumerable<string>";
    }
}

我可以改变我的接口要求的IList要实施(选项1),但谁可以实现该接口并可以传递到ConsumerA类的数量限制。或者,我可以改变的实现(类迈德特),使其返回,而不是一个List(选项2)一个IEnumerable,但随后ConsumerB将不得不重新编写。

I could change my interface to require IList to be implemented (option 1), but that limits who can implement the interface and the number of classes that can be passed into ConsumerA. Or, I could change implementation (class MyData) so that it returns an IEnumerable instead of a List (option 2), but then ConsumerB would have to be rewritten.

这似乎是C#中的缺点,除非有人能赐教。

This seems to be a shortcoming of C# unless someone can enlighten me.

推荐答案

不幸的是,返回类型必须匹配。您正在寻找的被称为返回类型协方差什么和C#不支持。

Unfortunately, the return type must match. What you are looking for is called 'return type covariance' and C# doesn't support that.

<一个href=\"http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=90909\">http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=90909

埃里克利珀,在C#编译器团队的高级开发人员,提到在他的博客,他们不打算支持的返回类型协方差。

Eric Lippert, senior developer on C# Compiler team, mentions on his blog that they don't plan to support return type covariance.

这种差异被称为
  返回类型协方差。正如我
  在这个系列中提到的早期,(一)
  这个系列是不是那种
  方差,和(b)我们没有计划
  C#实现那种方差。
  

"That kind of variance is called "return type covariance". As I mentioned early on in this series, (a) this series is not about that kind of variance, and (b) we have no plans to implement that kind of variance in C#. "

<一个href=\"http://blogs.msdn.com/ericlippert/archive/2008/05/07/covariance-and-contravariance-part-twelve-to-infinity-but-not-beyond.aspx\">http://blogs.msdn.com/ericlippert/archive/2008/05/07/covariance-and-contravariance-part-twelve-to-infinity-but-not-beyond.aspx

这是值得一读的协变和逆变Eric的文章。

It's worth reading Eric's articles on covariance and contravariance.

<一个href=\"http://blogs.msdn.com/ericlippert/archive/tags/Covariance+and+Contravariance/default.aspx\">http://blogs.msdn.com/ericlippert/archive/tags/Covariance+and+Contravariance/default.aspx

这篇关于&QUOT;接口没有实现&QUOT;返回派生类型时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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