首页 > 代码库 > Lamda所有的Capture均是引用

Lamda所有的Capture均是引用

Lamda所有的Capture均是引用

  下面的程序,两个lambda 引用的实际上是同一个a、b。连int都能引用了,说明Lambda对所有类型都是以引用来处理。

 1 int a = 0; 2         int b = 1; 3         Func<int> exe = ()=>{ 4             int tmp = a; 5             a = b; 6             b = tmp+b; 7             return a; 8         }; 9         Debug.Log("a1 = " + exe());10         Debug.Log("a2 = " + exe());11         Debug.Log("a3 = " + exe());12         Debug.Log("a4 = " + exe());13         Debug.Log("a5 = " + exe());14 15         Func<int> exe2 = ()=>{16             int tmp = a;17             a = b;18             b = tmp+b;19             return a;20         };21         Debug.Log("a1 = " + exe2());22         Debug.Log("a2 = " + exe2());23         Debug.Log("a3 = " + exe2());24         Debug.Log("a4 = " + exe2());25         Debug.Log("a5 = " + exe2());

  结果为:

  技术分享

Lamda所有的Capture均是引用