前几天在写一个api接口,须要对衣物表进行分页查询,查询的output须要返回两个信息,一个是 totalCount,一个是 clothesList,在之前我可能须要封装一个 PagedClothes 类,以下代码:api
public class PagedClothes { public int TotalCount { get; set; } public List<Clothes> ClothesList { get; set; } } static PagedClothes GetPageList() { return new PagedClothes() { TotalCount = 100, ClothesList = new List<Clothes>() { } }; }
在 C# 7.0 以后若是以为封装一个类太麻烦或者没这个必要,能够用快餐写法,以下代码:ide
static (int, List<Clothes>) GetPageList() { return (10, new List<Clothes>() { }); }
这里的 (int, List<Clothes>)
是什么意思呢? 懂的朋友看到 (x,y)
立刻就会想到它是 .NET 引入的一个新类:ValueTuple,接下来的问题就是真的是ValueTuple吗? 用ILSpy 看看 IL 代码:工具
.method private hidebysig static valuetype [System.Runtime]System.ValueTuple`2<int32, class [System.Collections]System.Collections.Generic.List`1<class ConsoleApp2.Clothes>> GetPageList () cil managed { IL_0000: nop IL_0001: ldc.i4.s 10 IL_0003: newobj instance void class [System.Collections]System.Collections.Generic.List`1<class ConsoleApp2.Clothes>::.ctor() IL_0008: newobj instance void valuetype [System.Runtime]System.ValueTuple`2<int32, class [System.Collections]System.Collections.Generic.List`1<class ConsoleApp2.Clothes>>::.ctor(!0, !1) IL_000d: stloc.0 IL_000e: br.s IL_0010 IL_0010: ldloc.0 IL_0011: ret } // end of method Program::GetPageList
从 GetPageList
方法的 IL 代码能够很容易的看出方法返回值和return处都有 System.ValueTuple
标记,今后之后你可能就会觉得 (x,y) 就是 ValueTuple 的化身 ,是吧,问题就出如今这里,这个经验靠谱吗?this
为了去验证这个经验是否靠谱,我须要举几个例子:3d
为了更加清楚的表述,先上代码:code
(int totalCount, List<Clothes> orders) = GetPageList(); static (int, List<Clothes>) GetPageList() { return (10, new List<Clothes>() { }); }
如今已经知道当 (x,y) 做为方法返回值的时候在IL层面会被化做 ValueTuple,那 (x,y) 做为接受返回值的时候又是什么意思呢? 还会和 ValueTuple 有关系吗? 为了去验证,能够用反编译能力弱点的 dnspy 去看看反编译后的代码:blog
从图中能够看出,当用 (x,y) 模式接收的时候,貌似就是实现映射赋值 或者 叫作拆解赋值,是否是有一点模糊,这个 (x,y) 貌似和 ValueTuple 有关系,又貌似和 ValueTuple 不要紧,不过不要紧,继续看下一个例子。dns
(x,y) 也能够出如今 foreach 中,相信第一次看到这么玩仍是有一点吃惊的,以下代码:接口
var dict = new Dictionary<int, string>(); foreach ((int k, string v) in dict) { }
接下来继续用 dnspy 反编译一下:图片
我去,这回就清晰了,从图中能够看出,我写的 (x,y) 压根就没有 ValueTuple 的影子,说明在这个场景下二者并无任何关系,也就是说一样是 (x,y) ,放在不一样位置具备不一样的表现形式,这就很让人琢磨不透了, 可能有些朋友不死心,想看一下 Deconstruct
到底干了什么,知道的朋友应该明白这个新玩法叫作解构方法,继续看代码:
public readonly struct KeyValuePair<TKey, TValue> { private readonly TKey key; private readonly TValue value; public void Deconstruct(out TKey key, out TValue value) { key = Key; value = Value; } }
有些抬杠的朋友会发现一个规律,貌似 (x,y) 放在赋值语句的左边都和 ValueTuple 没有任何关系,放在右边可能会有奇迹发生,那究竟是不是这样呢? 继续硬着头皮举例子呗。
public class Point { public int X { get; set; } public int Y { get; set; } public Point(int x, int y) { (X, Y) = (x, y); } }
嘿嘿,看这句: (X, Y) = (x, y)
里的 (x,y) 是 ValueTuple 的化身吗? 我猜你确定是懵逼状态,是吧,亦真亦假,虚虚实实,证据仍是继续反编译看呗。
我去,又是和 ValueTuple 一点关系都没有,啥玩意嘛,乱七八糟的,莫名其妙。
说 (x,y) 是元组吧,放在 return 中就变成了 ValueTuple ,说 (x,y) 不是元组吧,放在其余处还真就不是的,是否是很疑惑,为了更加形象,在 Point 中再增长一个 Test 方法,对照一下源码和反编译的代码:
//原始的: public class Point { public int X { get; set; } public int Y { get; set; } public Point(int x, int y) { (X, Y) = (x, y); } public (int x, int y) Test() { return (X, Y); } } //反编译的: public class Point { public int X { get; set; } public int Y { get; set; } public Point(int x, int y) { this.X = x; this.Y = y; } [return: TupleElementNames(new string[] { "x", "y" })] public ValueTuple<int, int> Test() { return new ValueTuple<int, int>(this.X, this.Y); } }
反正我已经恼火了,就这样吧,少用经验推理,多用工具挖一挖,这样才靠谱!