C#7 tuple & async

Oszkar picture Oszkar · Mar 9, 2017 · Viewed 19.8k times · Source

Old format:

private async Task<Tuple<SomeArray[], AnotherArray[], decimal>>         
GetInvoiceDetailAsync(InvoiceHead invoiceHead) { ... }

How can you do that in C#7 with new tuples format?

Answer

David Arno picture David Arno · Mar 9, 2017

Step 1: add the ValueTuple nuget package to your project.

Step 2: as Lucas says in his comment, change the syntax to:

private async Task<(SomeArray[], AnotherArray[], decimal)>GetInvoiceDetailAsync(
    InvoiceHead invoiceHead) 
{ 
    ...
    return (x, y, z); 
}

Note though that you can also then add names to those tuple items, along the lines of:

private async Task<(SomeArray[] x, AnotherArray[] y, decimal z)>GetInvoiceDetailAsync(
    InvoiceHead invoiceHead) 
{ 
    ... 
    return (x, y, z); 
}

And you can then access them as those names, rather than Item1, Item2 and Item3.