简体   繁体   English

是否可以解构ValueTuple参数?

[英]Is it possible to deconstruct out ValueTuple parameters?

Is it possible to deconstruct a tuple which isn't returned from a method, but is an out parameter? 是否有可能解构一个未从方法返回的元组,但是是一个out参数? I'm not sure I'm expressing myself correctly or even using the right terms, so here's some examples: 我不确定我是正确表达自己还是使用正确的术语,所以这里有一些例子:

void OutMethod(out (int aNumber, string someText) output)
    => output = (15, "yo");

void Usage()
{
    {
        // Works, of course.
        OutMethod(out var tuple);

        // But *slightly* aesthetically unappealing to use.
        var usage = $"{tuple.someText}: {tuple.aNumber}";
    }

    {
        // Would be awesome, but doesn't work.
        OutFunction(out var(number, text));
    }

    {
        // Would be awesome too, but doesn't work.
        OutFunction(out (var number, var text));
    }

    {
        // This doesn't work either.
        OutFunction((out var number, out var text));
    }

    {
        // Not even this.
        OutFunction((out int number, out string text));
    }

    {
        // Or this.
        OutMethod(out (int number, string text));
    }

    {
        // Or this.
        int number;
        string text;
        OutMethod(out (number, text));
    }
}

BTW, not complaining. BTW,不抱怨。 Just wondering if I'm missing something. 只是想知道我是否遗漏了什么。

This currently isn't possible. 目前这是不可能的。 Further, according to this comment on the CSharplang github repo : 此外,根据这篇关于CSharplang github repo的评论

This was something that we took up in the LDM [Language Design Meetings] when we were triaging and decided against doing, at least for the near future... 这是我们在LDM [语言设计会议]中讨论的事情,当我们进行分类并决定不做,至少在不久的将来......

So it's likely to remain "isn't possible" for some time to come. 因此,在未来一段时间内,它可能仍然“不可能”。

While the other answer is correct in that it can't be done in one line as part of the function call, you can of course manually deconstruct the out parameter as needed: 虽然另一个答案是正确的,因为它不能作为函数调用的一部分在一行中完成,您当然可以根据需要手动解构out参数:

OutMethod(out var tuple);
(int aNumber, string someText) = tuple;
var usage = $"{someText}: {aNumber}";

This will at least address your usage concerns. 这至少可以解决您的使用问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM