Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to deconstruct out ValueTuple parameters?

Tags:

c#

c#-7.0

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.

like image 857
relatively_random Avatar asked Dec 15 '17 13:12

relatively_random


People also ask

What is ValueTuple?

ValueTuple is a structure introduced in C# 7.0 which represents the value type Tuple. It is already included in . NET Framework 4.7 or higher version. It allows you to store a data set which contains multiple values that may or may not be related to each other.

What is deconstruct method in C#?

Deconstruction is a process of splitting a variable value into parts and storing them into new variables. This could be useful when a variable stores multiple values such as a tuple.

Is there Destructuring in C#?

Right now it is not possible. Yup, C# 7.0 supports deconstruction: blogs.msdn.microsoft.com/dotnet/2016/08/24/… Tuples are the correct way to do this now that C#7 is here.

Is tuple value type?

Tuple types are value types; tuple elements are public fields. That makes tuples mutable value types.


2 Answers

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.

like image 151
David Arno Avatar answered Oct 16 '22 02:10

David Arno


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.

like image 36
ketura Avatar answered Oct 16 '22 00:10

ketura