Is it possible to deconstruct out ValueTuple parameters?

白昼怎懂夜的黑 提交于 2019-12-12 10:31:21

问题


Is it possible to deconstruct a tuple which isn't returned from a method, but is an out parameter? 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. Just wondering if I'm missing something.


回答1:


This currently isn't possible. Further, according to this comment on the 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...

So it's likely to remain "isn't possible" for some time to come.




回答2:


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:

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

This will at least address your usage concerns.



来源:https://stackoverflow.com/questions/47833394/is-it-possible-to-deconstruct-out-valuetuple-parameters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!