.NET:C# 如何實現的閉包?

背景

C# 在編譯器層面爲咱們提供了閉包機制(Java7 和 Go 也是這種思路),本文簡單的作個解釋。閉包

背景知識

你必須瞭解:引用類型、值類型、引用、對象、值類型的值(簡稱值)。工具

關於引用、對象和值在內存的分配有以下幾點規則:測試

  • 對象分配在堆中。
  • 做爲字段的引用分配在堆中(內嵌在對象中)。
  • 做爲局部變量(參數也是局部變量)的引用分配在棧中。
  • 做爲字段的值分配在堆中(內嵌在對象中)。
  • 做爲局部變量(參數也是局部變量)的值用分配在棧中。
  • 局部變量只能存活於所在的做用域(方法中的大括號肯定了做用域的長短)。

注:按值傳遞和按引用傳遞也是須要掌握的知識點,C# 默認是按值傳遞的。this

閉包示例

測試代碼spa

 1         private static void Before()
 2         {
 3             Action[] actions = new Action[10];
 4 
 5             for (var i = 0; i < actions.Length; i++)
 6             {
 7                 actions[i] = () =>
 8                 {
 9                     Console.WriteLine(i);
10                 };
11             }
12 
13             foreach (var item in actions)
14             {
15                 item();
16             }
17         }

輸出結果code

編譯器幫咱們作了是什麼?

編譯器幫咱們生成的代碼(我本身寫的,能夠使用 Reflector 工具本身查看)對象

 1         private static void After()
 2         {
 3             Action[] actions = new Action[10];
 4 
 5             var anonymous = new AnonymousClass();
 6 
 7             for (anonymous.i = 0; anonymous.i < actions.Length; anonymous.i++)
 8             {
 9                 actions[anonymous.i ] = anonymous.Action;
10             }
11 
12             foreach (var item in actions)
13             {
14                 item();
15             }
16         }
17 
18         class AnonymousClass
19         {
20             public int i;
21 
22             public void Action()
23             {
24                 Console.WriteLine(this.i);
25             }
26         }

如何修復上面的問題?

上面的例子不是咱們指望的輸出,讓咱們給出兩種修改方案:blog

第一種(借鑑JS)內存

 1         private static void Fix()
 2         {
 3             Action[] actions = new Action[10];
 4 
 5             for (var i = 0; i < actions.Length; i++)
 6             {
 7                 new Action<int>((j) =>
 8                 {
 9                     actions[i] = () =>
10                     {
11                         Console.WriteLine(j);
12                     };
13                 })(i);
14             }
15 
16             foreach (var item in actions)
17             {
18                 item();
19             }
20         }

第二種作用域

 1         public static void Fix2()
 2         {
 3             Action[] actions = new Action[10];
 4 
 5             for (var i = 0; i < actions.Length; i++)
 6             {
 7                 var j = i;
 8                 
 9                 actions[i] = () =>
10                 {
11                     Console.WriteLine(j);
12                 };
13             }
14 
15             foreach (var item in actions)
16             {
17                 item();
18             }
19         }

分析

編譯器將閉包引用的局部變量轉換爲匿名類型的字段,致使了局部變量分配在堆中。

備註

C# 編譯器幫咱們作了很是多的工做,如:自動屬性、類型推斷、匿名類型、匿名委託、Lamda 表達式、析構方法、await 和 sync、using、對象初始化表達式、lock、默認參數 等等,這些統稱爲「語法糖」。

相關文章
相關標籤/搜索