本文介绍了传递 ValueTuple 而不是参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以这样做(或者我可能需要特定版本的 C#)?
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()); 推荐答案是的,你可以使用 Named ValueTuples C# 7.1 ,甚至只是一个 Local Method 如果合适
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));或者只是作为本地方法
void TriSum((int a, int b, int c) t) => Console.WriteLine(t.a + t.b + t.c); TriSum((1, 2, 3));更多推荐
传递 ValueTuple 而不是参数
发布评论