<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          如何在 C#9 中使用 static 匿名函數(shù)?

          共 4181字,需瀏覽 9分鐘

           ·

          2021-03-31 21:32

          匿名函數(shù) 在 C# 中已經(jīng)出現(xiàn)很多年了,雖然匿名函數(shù)用起來很爽,但代價是不小的,為了避免不必要那些你意想不到的內(nèi)存分配,這就是為什么 C#9 中引入 static 匿名函數(shù)的原因,這篇文章我們將會討論如何使用 靜態(tài)匿名函數(shù) 以及為什么要用。

          匿名方法的代價

          匿名方法代價不低,因為它有 委托調用 方面的開銷,什么意思呢?如果你的 lambda 里需要捕獲封閉方法的局部變量或者參數(shù),那么就會存在兩種堆分配,一種是委托上的分配,另一種是閉包上的分配,如果你的 lambda 僅僅捕獲一個封閉方法的實例狀態(tài),那么僅會有委托分配,如果你的 lambda 什么都不捕獲或者僅捕獲一個靜態(tài)狀態(tài),那么就沒有任何分配。

          如果你有點懵的話,使用下面的例子加深理解。


          int y = 1;
          MyMethod(x => x + y);

          上面代碼的 lambda 中需要獲取 y,所以就有了意想不到的堆分配,要想解決,可以將 y 定義為 const 或者 static 來避免這種不必要的分配開銷,修改代碼如下:


          const int y = 1;
          MyMethod(static x => x + y);

          為了避免這種不必要和浪費內(nèi)存的分配,可以在 lambda 上使用 static 關鍵詞 及 變量上標注 const,值得注意的是,static 匿名函數(shù)不能訪問封閉方法的局部變量和參數(shù)和 this 指針,但可以引用它的 靜態(tài)方法常量

          C#9 中使用靜態(tài)匿名方法

          先看一下例子:


              public class Demo
              {
                  private string formattedText = "{0} It was developed by Microsoft's Anders Hejlsberg in the year 2000.";
                  void DisplayText(Func<stringstring> func)
                  {
                      Console.WriteLine(func("C# is a popular programming language."));
                  }
                  public void Display()
                  {
                      DisplayText(text => string.Format(formattedText, text));
                      Console.Read();
                  }
              }
              class Program
              {
                  static void Main(string[] args)
                  {
                      new Demo().Display();
                      Console.Read();
                  }
              }

          上面的例子中,formattedText變量會被 DisplayText 方法中的 func 所捕獲,這也就意味著它會產(chǎn)生你意料之外的內(nèi)存分配,把程序跑起來之后,會看到如下的輸出。

          為了避免這種內(nèi)存分配,你只需要做兩件事。

          • formattedText 上標記 const。

          • lambda 上標記 static。

          所以修改后的代碼如下:


              public class Demo
              {
                  private const string formattedText = "{0} It was developed by Microsoft's Anders Hejlsberg in the year 2000.";
                  void DisplayText(Func<stringstring> func)
                  {
                      Console.WriteLine(func("C# is a popular programming language."));
                  }
                  public void Display()
                  {
                      DisplayText(static text => string.Format(formattedText, text));
                      Console.Read();
                  }
              }

          現(xiàn)在就沒有任何你意料之外的分配了,我想這也是你想要的。

          現(xiàn)在你可以使用 static + const 組合來提升應用程序性能了,同時也可以有效的阻止在 lambda 中誤用封閉方法中的局部變量和參數(shù)引發(fā)的不必要開銷。

          譯文鏈接:https://www.infoworld.com/article/3609850/how-to-work-with-static-anonymous-functions-in-csharp-9.html







          回復 【關閉】
          回復 【實戰(zhàn)】獲取20套實戰(zhàn)源碼
          回復 【被刪】
          回復 【訪客】
          回復 【小程序】學獲取15套【入門+實戰(zhàn)+賺錢】小程序源碼
          回復 【python】學微獲取全套0基礎Python知識手冊
          回復 【2019】獲取2019 .NET 開發(fā)者峰會資料PPT
          回復 【加群】加入dotnet微信交流群

          強烈推薦:超全C#幫助類,提升效率就靠它


          @所有人,20個Q幣,趕快領取!


          瀏覽 40
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  中国黄色视频在线看 | 热久久综合免费频99热 | 亚洲视频中文字幕在线播放 | 日日操夜夜撸 | 黄色视频免费久久 |