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

查看:29
本文介绍了子类返回类型的 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
}

感谢您的回复.

推荐答案

UPDATE:这个答案写于 2011 年.经过二十年人们为 C# 提出返回类型协变的建议,看起来它最终会被实现;我比较惊讶.见底部https://devblogs.microsoft.com/dotnet/welcome-to-c-9-0/ 用于公告;我相信细节会随之而来.

UPDATE: This answer was written in 2011. After two decades of people proposing return type covariance for C#, it looks like it will finally be implemented; I am rather surprised. See the bottom of https://devblogs.microsoft.com/dotnet/welcome-to-c-9-0/ for the announcement; I'm sure details will follow.

首先,返回类型逆变没有任何意义;我认为您在谈论返回类型协方差.

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

查看此问题了解详情:

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# 可版本化的目标,因为它引入了另一种形式的脆弱基类问题,Anders 认为它​​不是一个有趣或有用的特性,如果你真的想要它,您可以通过编写小助手方法使其工作.(这正是 C++ 的 CIL 版本所做的.)

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天全站免登陆