Tuple & ValueTuple

偶然间看到Tuple,查询了一下,顺便还发现了ValueTuple,可以指定多个参数,还可以参数嵌套Tuple,作为方法返回值,或参数。。。方便。

按照Tuple写的api

var relist = new List<Tuple<string, string, decimal>>();

relist.Add(new Tuple<string, string, decimal>("PM2.5", "优", 1111));
relist.Add(new Tuple<string, string, decimal>("PM10", "优", 333));

返回结果

[
{"Item1":"PM2.5","Item2":"优","Item3":27.0},
{"Item1":"PM10","Item2":"优","Item3":29.0},
{"Item1":"O₃","Item2":"优","Item3":79.0},
{"Item1":"NO₂","Item2":"优","Item3":2.0},
{"Item1":"SO₂","Item2":"优","Item3":85.0}
]

不能定义指定名称,有点恶心。

ValueTuple的基本用法和Tuple一致,但是多了一些优化

        static (string , bool , decimal ) GetUser()
        {
            return ("c", true, 12.59m);
        }


        static (string name, bool sex, decimal height) GetNewUser()
        {
            return ("c", true, 12.59m);
        }

ValueTuple的简写,方便多了,而且还可以自定义返回值名字,太TMD爽了。

            var t = GetUser();
            Console.WriteLine(t.Item1);
            Console.WriteLine(t.Item2);
            Console.WriteLine(t.Item3);

            var t2 = GetNewUser();
            Console.WriteLine(t2.name);
            Console.WriteLine(t2.sex);
            Console.WriteLine(t2.height);

        static List<(string name, bool sex, decimal height)> GetNewUser()
        {
            List <(string name, bool sex, decimal height)> list = new List<(string name, bool sex, decimal height)>();
            list.Add(("c1", true, 12.59m));
            list.Add(("c2", true, 12.59m));
            list.Add(("c3", true, 12.59m));


            return list;
        }

感觉以后out ref之类的可以不用了

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值