是什么让 ValueTuple 协变?

What makes ValueTuple covariant?

这在 C# 7.3(框架 4.8)中正确编译:

(string, string) s = ("a", "b");
(object, string) o = s;

我知道这是以下内容的语法糖,它也可以正确编译:

ValueTuple<string, string> s = new ValueTuple<string, string>("a", "b");
ValueTuple<object, string> o = s;

因此,似乎可以为 ValueTuples 赋值 covariantly太棒了

不幸的是,我不明白为什么:我的印象是C#only supported covariance on interfaces and delegatesValueType 两者都不是。

事实上,当我尝试用我自己的代码复制这个功能时,我失败了:

struct MyValueTuple<A, B>
{
    public A Item1;
    public B Item2;

    public MyValueTuple(A item1, B item2)
    {
        Item1 = item1;
        Item2 = item2;
    }
}

...

MyValueTuple<string, string> s = new MyValueTuple<string, string>("a", "b");
MyValueTuple<object, string> o = s;
// ^ Cannot implicitly convert type 'MyValueTuple<string, string>' to 'MyValueTuple<object, string>'

那么,为什么 ValueTuples 可以协变分配,而 MyValueTuples 不能?

我相信这里实际发生的是一个解构赋值。元组赋值将尝试隐式转换其组件,因为可以将 string 赋值给 object,这就是这里发生的情况。

The language supports assignment between tuple types that have the same number of elements, where each right-hand side element can be implicitly converted to its corresponding left-hand side element. Other conversions aren't considered for assignments.

Source

See it on sharplab.io