“Interface not implemented” when Returning Derived Type

前端 未结 12 833
野趣味
野趣味 2020-11-27 21:11

The following code:

public interface ISomeData
{
    IEnumerable Data { get; }
}

public class MyData : ISomeData
{
    private List

        
12条回答
  •  执念已碎
    2020-11-27 21:49

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

    http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=90909

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

    "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#. "

    http://blogs.msdn.com/ericlippert/archive/2008/05/07/covariance-and-contravariance-part-twelve-to-infinity-but-not-beyond.aspx

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

    http://blogs.msdn.com/ericlippert/archive/tags/Covariance+and+Contravariance/default.aspx

提交回复
热议问题