In C# 7 is it possible to deconstruct tuples as method arguments

bradgonesurfing picture bradgonesurfing · Jan 11, 2017 · Viewed 21.2k times · Source

For example I have

private void test(Action<ValueTuple<string, int>> fn)
{
    fn(("hello", 10));
}

test(t => 
 {
    var (s, i) = t;
    Console.WriteLine(s);
    Console.WriteLine(i);
});

I would like to write something like this

private void test(Action<ValueTuple<string, int>> fn)
{
    fn(("hello", 10));
}

test((s,i) => 
{
    Console.WriteLine(s);
    Console.WriteLine(i);
});

Is this possible with some proper notation?

Answer

Paulo Morgado picture Paulo Morgado · Jan 11, 2017

You can shorten it to:

void test( Action<ValueTuple<string, int>> fn)
{
    fn(("hello", 10));
}

test(((string s, int i) t) =>
{
    Console.WriteLine(t.s);
    Console.WriteLine(t.i);
});

Hopefully, one day we might be able to splat the parameters from a tuple to the method invocation:

void test(Action<ValueTuple<string, int>> fn)
{
    fn(@("hello", 10)); // <-- made up syntax
}

test((s, i) =>
{
    Console.WriteLine(s);
    Console.WriteLine(i);
});

But not at the moment.