1. <tfoot id='Ywry4'></tfoot>

      <i id='Ywry4'><tr id='Ywry4'><dt id='Ywry4'><q id='Ywry4'><span id='Ywry4'><b id='Ywry4'><form id='Ywry4'><ins id='Ywry4'></ins><ul id='Ywry4'></ul><sub id='Ywry4'></sub></form><legend id='Ywry4'></legend><bdo id='Ywry4'><pre id='Ywry4'><center id='Ywry4'></center></pre></bdo></b><th id='Ywry4'></th></span></q></dt></tr></i><div id='Ywry4'><tfoot id='Ywry4'></tfoot><dl id='Ywry4'><fieldset id='Ywry4'></fieldset></dl></div>
        <bdo id='Ywry4'></bdo><ul id='Ywry4'></ul>
    2. <small id='Ywry4'></small><noframes id='Ywry4'>

      <legend id='Ywry4'><style id='Ywry4'><dir id='Ywry4'><q id='Ywry4'></q></dir></style></legend>

        函数委托与函数

        时间:2023-11-11
        <legend id='jW0hQ'><style id='jW0hQ'><dir id='jW0hQ'><q id='jW0hQ'></q></dir></style></legend>
      1. <small id='jW0hQ'></small><noframes id='jW0hQ'>

        <tfoot id='jW0hQ'></tfoot>

          <tbody id='jW0hQ'></tbody>
          • <bdo id='jW0hQ'></bdo><ul id='jW0hQ'></ul>

              <i id='jW0hQ'><tr id='jW0hQ'><dt id='jW0hQ'><q id='jW0hQ'><span id='jW0hQ'><b id='jW0hQ'><form id='jW0hQ'><ins id='jW0hQ'></ins><ul id='jW0hQ'></ul><sub id='jW0hQ'></sub></form><legend id='jW0hQ'></legend><bdo id='jW0hQ'><pre id='jW0hQ'><center id='jW0hQ'></center></pre></bdo></b><th id='jW0hQ'></th></span></q></dt></tr></i><div id='jW0hQ'><tfoot id='jW0hQ'></tfoot><dl id='jW0hQ'><fieldset id='jW0hQ'></fieldset></dl></div>
                • 本文介绍了函数委托与函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有人可以告诉我使用委托而不是调用函数本身的优势,如下所示(或者换句话说,为什么选择选项 A 而不是选项 B)?昨晚我在查看某人的 linq 代码,他们有类似于选项 A 的内容,但它被用于返回已编译的 linq 查询.

                  Can someone tell me the advantages of using a delegate as opposed to calling the function itself as shown below (or in other words why choose Option A over Option B)? I was looking at someone's linq code last night and they had something similar to Option A but it was being used to return a compiled linq query.

                  我意识到前者现在可以传递给其他功能......只是不确定它的实用性.顺便说一句,我意识到这不会按原样编译.在发布之前取消注释其中一个功能.TYIA

                  I realize the former can now be passed around to other functions.. just not sure of its practicality. BTW, I realize this wouldn't compile as-is.. uncommented one of the functions before posting. TYIA

                  class Program
                  {
                      static void Main(string[] args)
                      {   
                          Console.WriteLine(SayTwoWords("Hello", "World"));
                          Console.ReadKey();
                      }
                  
                      // Option A
                      private static Func<string, string, string>
                          SayTwoWords = (a, b) => String.Format("{0} {1}", a, b);
                  
                      // Option B
                      private static string SayTwoWords(string a, string b)
                      {
                          return String.Format("{0} {1}", a, b);
                      }        
                  }
                  

                  ************编辑************

                  ************EDIT************

                  不确定它是否更好地解释了我的问题,但这里是最初让我想到这个的代码类型的示例:

                  Not sure if it explains my question better but here is an example of the type of code that originally got me thinking about this:

                  public static class clsCompiledQuery
                  {
                      public static Func<DataContext, string, IQueryable<clsCustomerEntity>>
                          getCustomers = CompiledQuery.Compile((DataContext db, string strCustCode)
                              => from objCustomer in db.GetTable<clsCustomerEntity>()
                              where objCustomer.CustomerCode == strCustCode
                              select objCustomer);
                  }
                  

                  这样写函数有什么好处吗?

                  Is there any advantage to writing a function in this way?

                  推荐答案

                  你贴的代码没有优势.在您的代码中,使用委托只会增加复杂性以及额外的运行时成本 - 所以您最好直接调用该方法.

                  There is no advantage in the code you posted. In your code, using the delegate just adds complexity as well as an extra runtime cost - so you're better off just calling the method directly.

                  但是,委托有很多用途.传递"给其他方法是主要用法,但存储一个函数并在以后使用它也非常有用.

                  However, delegates have many uses. "Passing around" to other methods is the primary usage, though storing a function and using it later is also very useful.

                  LINQ 完全建立在这个概念之上.当你这样做时:

                  LINQ is built on top of this concept entirely. When you do:

                  var results = myCollection.Where(item => item == "Foo");
                  

                  您将委托(定义为 lambda:item => item == "Foo")传递给 LINQ 库中的 Where 函数.这就是让它正常工作的原因.

                  You're passing a delegate (defined as a lambda: item => item == "Foo") to the Where function in the LINQ libraries. This is what makes it work properly.

                  这篇关于函数委托与函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:具有未知类型的 CreateDelegate 下一篇:Delegate.CreateDelegate() 和泛型:错误绑定到目标方法

                  相关文章

                  • <bdo id='2rW4g'></bdo><ul id='2rW4g'></ul>
                • <i id='2rW4g'><tr id='2rW4g'><dt id='2rW4g'><q id='2rW4g'><span id='2rW4g'><b id='2rW4g'><form id='2rW4g'><ins id='2rW4g'></ins><ul id='2rW4g'></ul><sub id='2rW4g'></sub></form><legend id='2rW4g'></legend><bdo id='2rW4g'><pre id='2rW4g'><center id='2rW4g'></center></pre></bdo></b><th id='2rW4g'></th></span></q></dt></tr></i><div id='2rW4g'><tfoot id='2rW4g'></tfoot><dl id='2rW4g'><fieldset id='2rW4g'></fieldset></dl></div>

                    <legend id='2rW4g'><style id='2rW4g'><dir id='2rW4g'><q id='2rW4g'></q></dir></style></legend>
                    1. <tfoot id='2rW4g'></tfoot>
                    2. <small id='2rW4g'></small><noframes id='2rW4g'>