偶然间看到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之类的可以不用了