传递 ValueTuple 而不是参数 [英] Pass ValueTuple instead of arguments

查看:46
本文介绍了传递 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 而不是参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆