1

In my c# project a method returns a tuple. Is it possible to deconstruct the result of this method immediately inline and use it as arguments for another method call? I imagine something similar to the ... operator in JavaScript.

public (string a, string b) GetTupelResult() {
    return ("result a", "result b");
}

public void MethodWithTwoStringParameters(string a, string b) {
    Debug.WriteLine(a);
    Debug.WriteLine(b);
}

public void Main() {
    // Deconstruct first, call later ==> WORKS
    (string a, string b) = GetTupelResult();
    MethodWithTwoStringParameters(a, b);

    // Inline solution ==> IS SUCH A THING POSSIBLE?
    MethodWithTwoStringParameters(...GetTupelResult());
}

Is there a way to achieve a single-line solution in c# without modifying GetTupelResult and MethodWithTwoStringParameters?

I know, that I could modify MethodWithTwoStringParameters to accept a tupel as parameter, or build a wrapper method/overload. But that is not the point I am trying to make.

André Reichelt
  • 1,484
  • 18
  • 52
  • Does this answer your question? [Deconstruct a C# Tuple](https://stackoverflow.com/questions/47217213/deconstruct-a-c-sharp-tuple) – Ishmaeel Sep 29 '20 at 09:50
  • @Ishmaeel No. Regular deconstruction, as described in that other question, works well in my code. I wonder, if there is a tupel-to-args converter/operator. – André Reichelt Sep 29 '20 at 09:52
  • 1
    @MathewHD Sure, but this would call the method twice. I just posted a minimal example. In reality, the GetTupelResult gathers multiple data from a database and returns a complex result with different object types instead of string. – André Reichelt Sep 29 '20 at 09:54
  • Does this answer your question? [Pass ValueTuple instead of arguments](https://stackoverflow.com/questions/55588765/pass-valuetuple-instead-of-arguments) ... still - no – Selvin Sep 29 '20 at 09:56
  • @Selvin Partially... It would require to modify the `MethodWithTwoStringParameters` method's parameters, or to build a wrapper for that. – André Reichelt Sep 29 '20 at 10:00
  • you had asked about *a single-line solution* so .. no – Selvin Sep 29 '20 at 10:01
  • @Selvin I've edited the question. Hopefully, it's more clear now. – André Reichelt Sep 29 '20 at 10:08
  • 1
    `GetTupelResult().With(MethodWithTwoStringParameters)` ... `public static class Ext1 { public static void With(this ValueTuple tuple, Action action) { var (a, b) = tuple; action(a, b); } }` [...click...](https://dotnetfiddle.net/Xm4hov) ... but seriously what for ... – Selvin Sep 29 '20 at 10:34
  • @Selvin Thank you. Nice to konw, that there is *some* way to do it. However, my intention of this question was, if there is a 1:1 equivalent of JS's `...` operator in c#, to simplify my existing code. As there doesn't seem to be one yet, I think that for readability purposes, I'd rather stick with the existing two-line solution I posted above. Thank you very much anyways! :) – André Reichelt Sep 29 '20 at 12:09

0 Answers0