传递 ValueTuple 而不是参数

编程入门 行业动态 更新时间:2024-10-26 22:19:08
本文介绍了传递 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 而不是参数

本文发布于:2023-11-05 19:24:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561645.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   参数   ValueTuple

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!