在子类返回类型的C#协方差 [英] C# Covariance on subclass return types

查看:225
本文介绍了在子类返回类型的C#协方差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道为什么变的返回类型不支持在C#中?甚至试图使用一个接口时,编译器会抱怨,这是不允许的。请看下面的例子。

Does anyone know why covariant return types are not supported in C#? Even when attempting to use an interface, the compiler complains that it is not allowed. See the following example.

class Order
{
    private Guid? _id;
    private String _productName;
    private double _price;

    protected Order(Guid? id, String productName, double price)
    {
        _id = id;
        _productName = productName;
        _price = price;
    }

    protected class Builder : IBuilder<Order>
    {
        public Guid? Id { get; set; }
        public String ProductName { get; set; }
        public double Price { get; set; }

        public virtual Order Build()
        {
            if(Id == null || ProductName == null || Price == null)
                throw new InvalidOperationException("Missing required data!");

            return new Order(Id, ProductName, Price);
        }
    }            
}

class PastryOrder : Order
{
    PastryOrder(Guid? id, String productName, double price, PastryType pastryType) : base(id, productName, price)
    {

    }

    class PastryBuilder : Builder
    {
        public PastryType PastryType {get; set;}

        public override PastryOrder Build()
        {
            if(PastryType == null) throw new InvalidOperationException("Missing data!");
            return new PastryOrder(Id, ProductName, Price, PastryType);
        }
    }
}

interface IBuilder<in T>
{
    T Build();
}

public enum PastryType
{
    Cake,
    Donut,
    Cookie
}

感谢您的任何答复。

Thanks for any responses.

推荐答案

首先,返回类型的逆变没有任何意义;我想你所谈论的返回类型的

First off, return type contravariance doesn't make any sense; I think you are talking about return type covariance.

请参阅此问题的详细信息:

See this question for details:

<一个href="http://stackoverflow.com/questions/5709034/does-c-sharp-support-return-type-covariance/5709191#5709191">Does支持C#返回类型的协方差?

您想知道为什么该功能尚未实现。 phoog是正确的;这一功能将不会实现,因为在这里没有人会付诸实施。一个必要但不充分条件是,该功能的好处超过了其成本。

You want to know why the feature is not implemented. phoog is correct; the feature is not implemented because no one here ever implemented it. A necessary but insufficient requirement is that the feature's benefits exceed its costs.

的成本是相当大的。不支持的功能本机运行时,它的工作原理直接针对我们的目标是使C#版本控制,因为它引入的脆基类问题的另一种形式,安德斯并不认为这是一个有趣或实用的功能,如果你真的想要的话,你可以把它写的小帮手方法的工作。 (这正是CIL版本的C ++一样。)

The costs are considerable. The feature is not supported natively by the runtime, it works directly against our goal to make C# versionable because it introduces yet another form of the brittle base class problem, Anders doesn't think it is an interesting or useful feature, and if you really want it, you can make it work by writing little helper methods. (Which is exactly what the CIL version of C++ does.)

的好处是小

高性价比,用一个简单的解决办法小利的特点得到分流走的非常快的。我们有高得多的优先级。

High cost, small benefit features with an easy workaround get triaged away very quickly. We have far higher priorities.

这篇关于在子类返回类型的C#协方差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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