将新元组转换为旧元组会产生编译错误

jim*_*own 6 c# tuples c#-7.0

使用下面的代码,

class Program
{
    static void Main(string[] args)
    {
        Tuple<int, int> test = TupleTest();

    }

    static (int, int) TupleTest()
    {
        return (1, 2);
    }
Run Code Online (Sandbox Code Playgroud)

我收到编译时错误.

错误CS0029无法将类型'(int,int)'隐式转换为'System.Tuple <int,int>'

是否意味着新版本的Tuple隐含地与旧版本不兼容?或者我在这里做错了什么?

Mat*_*rný 14

是的,你应该使用扩展方法ToTuple.所以在你的例子中......

class Program
{
    static void Main(string[] args)
    {
        Tuple<int, int> test = TupleTest().ToTuple();

    }

    static (int, int) TupleTest()
    {
        return (1, 2);
    }
Run Code Online (Sandbox Code Playgroud)