<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# 中的 Action, Func,Predicate

          共 6817字,需瀏覽 14分鐘

           ·

          2020-12-04 12:49

          譯文鏈接:https://www.infoworld.com/article/3057152/how-to-work-with-action-func-and-predicate-delegates-in-csharp.html?nsdr=true

          委托是一個(gè)類型安全的函數(shù)指針,它可以引用與委托具有相同簽名的方法。委托常用于實(shí)現(xiàn)回調(diào)方法或者事件機(jī)制,在C#中一般用 "delegate" 關(guān)鍵字聲明。你可以聲明一個(gè)和類平級(jí)的委托,也可以嵌套在類中。

          Func 和 Action 是什么,如何使用?

          兩者最基本的區(qū)別是,前者適合那些需要帶返回值的委托,后者適合那些不帶返回值的委托。

          Func 所引用的方法接收一個(gè)或者多個(gè)入?yún)⒉в幸粋€(gè)返回值,Action所引用的方法接收一個(gè)或者多個(gè)參數(shù)并且沒有返回值,換句話說,你的委托所引用的方法沒有返回值,這時(shí)候適合用 Action。

          Predicate所引用的方法接收一個(gè)或者多個(gè)泛型參數(shù)并且返回一個(gè) bool 值,你可以假定它等價(jià)于 Func ,Predicate 常用于對(duì) collection 進(jìn)行一組條件檢索。

          C# 中使用 Action

          你可以使用 委托 去實(shí)現(xiàn)事件和回調(diào)方法,C#委托非常類似于C++中的函數(shù)指針,但是 C# 中的 委托 是類型安全的,你可以將方法作為參數(shù)傳遞給委托從而讓委托指向該方法。

          下面的代碼片段展示了 Action 委托的語法結(jié)構(gòu)。


          Action

          接下來的代碼清單展示了如何使用 Action 委托,當(dāng)下面的代碼執(zhí)行結(jié)束后會(huì)在控制臺(tái)打印 Hello !!!

                  static void Main(string[] args)
                  {
                      Action<string> action = new Action<string>(Display);
                      action("Hello!!!");
                      Console.Read();
                  }
                  
                  static void Display(string message)
                  {
                      Console.WriteLine(message);
                  }

          C# 中使用 Func

          現(xiàn)在我們一起學(xué)習(xí)下 Func 委托,下面是 Func 的語法結(jié)構(gòu)。


          Func

          接下來的代碼片段展示了如何在 C# 中使用 Func 委托,最終方法會(huì)打印出 Hra(基本薪資的 40%) 的值,基本薪資是作為參數(shù)傳下去的,如下代碼所示:


                  static void Main(string[] args)
                  {
                      Func<intdouble> func = new Func<intdouble>(CalculateHra);
                      Console.WriteLine(func(50000));
                      Console.Read();
                  }
                  static double CalculateHra(int basic)
                  {
                      return (double)(basic * .4);
                  }

          值得注意的是,F(xiàn)unc 委托的第二個(gè)參數(shù)表示方法的返回值,在上面這個(gè)例子中,它就是計(jì)算后的 Hra 值,作為 double 型返回。

          C# 中使用 Predicate

          Predicate 委托常用于檢索 collection,下面是 Predicate 的語法結(jié)構(gòu)。

          Predicate
               
                

          值得注意的是, Predicate 差不多等價(jià)于 Func

          考慮下面的 Customer 實(shí)體類。


              class Customer
              {
                  public int Id { getset; }
                  public string FirstName { getset; }
                  public string LastName { getset; }
                  public string Address { getset; }
                  public string City { getset; }
                  public string State { getset; }
                  public string Country { getset; }
              }

          接下來生成一個(gè) customer 集合并且丟一些數(shù)據(jù)進(jìn)去,如下代碼:


                      List  custList =  new List ();
                      custList.Add( new Customer { Id =  1, FirstName =  "Joydip", LastName =  "Kanjilal", State =  "Telengana", City =  "Hyderabad", Address =  "Begumpet", Country =  "India" });
                      custList.Add( new Customer { Id =  2, FirstName =  "Steve", LastName =  "Jones", State =  "OA", City =  "New York", Address =  "Lake Avenue", Country =  "US" });

          接下來是完整的代碼片段展示了如何使用 Predicate 去檢索集合。


                  static void Main(string[] args)
                  {
                      List  custList =  new List ();
                      custList.Add( new Customer { Id =  1, FirstName =  "Joydip", LastName =  "Kanjilal", State =  "Telengana", City =  "Hyderabad", Address =  "Begumpet", Country =  "India" });
                      custList.Add( new Customer { Id =  2, FirstName =  "Steve", LastName =  "Jones", State =  "OA", City =  "New York", Address =  "Lake Avenue", Country =  "US" });
                      Predicate  hydCustomers = x => x.Id ==  1;
                      Customer customer = custList.Find(hydCustomers);
                      Console.WriteLine(customer.FirstName);
                      Console.Read();
                  }

          當(dāng)上面的代碼被成功執(zhí)行, 控制臺(tái)將會(huì)輸出 Joydip

          歡迎各位讀者加入微信群一起學(xué)習(xí)交流,
          在公眾號(hào)后臺(tái)回復(fù)“加群”即可~~


          瀏覽 11
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <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>
                  日韩A片免费在线观看 | a 视频在线播放 | 日韩无码高清视频看看看看 | 欧美日韩国产一级 | 小姨妈操爽了[28p] |