Pass ValueTuple instead of arguments

Troll the Legacy picture Troll the Legacy · Apr 9, 2019 · Viewed 6.9k times · Source

Is it possible to do in this way (or maybe I need specific version of C#)?

Function<int,int,int,int> triSum = (a,b,c) => {return a+b+c;};
var tup = (1,2,3);
triSum(tup); //passing one tuple instead of multiple args

Update: I mean passing tuple instead of separate arguments.

public void DoWrite(string s1, string s2)
{
Console.WriteLine(s1+s2);
}
public (string,string) GetTuple()
{
//return some of them
}
//few lines later
DoWrite(GetTuple());

Answer

TheGeneral picture TheGeneral · Apr 9, 2019

Yes you could use Named ValueTuples C# 7.1 , or even just a Local Method if it suits

Action<(int a, int b, int c)> triSum = t 
   => Console.WriteLine(t.a + t.b + t.c);

triSum((1, 2, 3));

or just as a local method

void TriSum((int a, int b, int c) t)
   => Console.WriteLine(t.a + t.b + t.c);

TriSum((1, 2, 3));