<tfoot id='x16uA'></tfoot>

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

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

      ΪʲôҪʹÓñí´ïʽ&lt;Func&lt;T&gt;&gt;¶ø²»ÊÇ Func T ?

      Why would you use Expressionlt;Funclt;Tgt;gt; rather than Funclt;Tgt;?(ΪʲôҪʹÓñí´ïʽlt;Funclt;Tgt;gt;¶ø²»ÊÇ Func T ?)
        <tbody id='Dkk6e'></tbody>

      <legend id='Dkk6e'><style id='Dkk6e'><dir id='Dkk6e'><q id='Dkk6e'></q></dir></style></legend>
        <bdo id='Dkk6e'></bdo><ul id='Dkk6e'></ul>
        <i id='Dkk6e'><tr id='Dkk6e'><dt id='Dkk6e'><q id='Dkk6e'><span id='Dkk6e'><b id='Dkk6e'><form id='Dkk6e'><ins id='Dkk6e'></ins><ul id='Dkk6e'></ul><sub id='Dkk6e'></sub></form><legend id='Dkk6e'></legend><bdo id='Dkk6e'><pre id='Dkk6e'><center id='Dkk6e'></center></pre></bdo></b><th id='Dkk6e'></th></span></q></dt></tr></i><div id='Dkk6e'><tfoot id='Dkk6e'></tfoot><dl id='Dkk6e'><fieldset id='Dkk6e'></fieldset></dl></div>
          • <small id='Dkk6e'></small><noframes id='Dkk6e'>

              <tfoot id='Dkk6e'></tfoot>

                ±¾ÎĽéÉÜÁËΪʲôҪʹÓñí´ïʽ&lt;Func&lt;T&gt;&gt;¶ø²»ÊÇ Func T ?µÄ´¦Àí·½·¨£¬¶Ô´ó¼Ò½â¾öÎÊÌâ¾ßÓÐÒ»¶¨µÄ²Î¿¼¼ÛÖµ£¬ÐèÒªµÄÅóÓÑÃÇÏÂÃæËæןú°æÍøµÄС±àÀ´Ò»Æðѧϰ°É£¡

                ÎÊÌâÃèÊö

                ÎÒÁ˽â lambda ÒÔ¼° Func ºÍ Action ίÍÐ.µ«ÊDZí´ïʽÄѵ¹ÎÒ.

                I understand lambdas and the Func and Action delegates. But expressions stump me.

                ÔÚʲôÇé¿öÏÂÄã»áʹÓà Expression<Func<T>> ¶ø²»ÊÇÆÕͨµÄ¾É Func<T>?

                In what circumstances would you use an Expression<Func<T>> rather than a plain old Func<T>?

                ÍƼö´ð°¸

                µ±ÄúÏ뽫 lambda ±í´ïʽÊÓΪ±í´ïʽÊ÷²¢²é¿´ËüÃÇÄÚ²¿¶ø²»ÊÇÖ´ÐÐËüÃÇʱ.ÀýÈ磬LINQ to SQL »ñÈ¡±í´ïʽ²¢½«Æäת»»ÎªµÈЧµÄ SQL Óï¾ä²¢½«ÆäÌá½»¸ø·þÎñÆ÷(¶ø²»ÊÇÖ´ÐÐ lambda).

                When you want to treat lambda expressions as expression trees and look inside them instead of executing them. For example, LINQ to SQL gets the expression and converts it to the equivalent SQL statement and submits it to server (rather than executing the lambda).

                ´Ó¸ÅÄîÉϽ²£¬Expression<Func<T>> Óë Func<T>ÍêÈ«²»Í¬.Func ±íʾһ¸ö delegate£¬Ëü¼¸ºõÊÇÒ»¸öÖ¸Ïò·½·¨µÄÖ¸Õ룬¶ø Expression ±íʾһ¸ö Ê÷Êý¾Ý½á¹¹ ÓÃÓÚ lambda ±í´ïʽ.Õâ¸öÊ÷ÐνṹÃèÊöÁË lambda ±í´ïʽµÄ×÷Ó㬶ø²»ÊÇʵ¼ÊµÄÊÂÇé.Ëü»ù±¾Éϱ£´æÓйرí´ïʽ¡¢±äÁ¿¡¢·½·¨µ÷ÓõÄ×éºÏµÄÊý¾Ý£¬......(ÀýÈ磬Ëü±£´æÖîÈçÕâ¸ö lambda ÊÇij¸ö³£Á¿+ij¸ö²ÎÊýÖ®ÀàµÄÐÅÏ¢).Äú¿ÉÒÔʹÓôËÃèÊö½«Æäת»»ÎªÊµ¼Ê·½·¨(ʹÓà Expression.Compile)»òʹÓÃËüÖ´ÐÐÆäËû²Ù×÷(Èç LINQ to SQL ʾÀý).½« lambdas ÊÓΪÄäÃû·½·¨ºÍ±í´ïʽÊ÷µÄÐÐΪ´¿´âÊDZàÒëʱµÄÊÂÇé.

                Conceptually, Expression<Func<T>> is completely different from Func<T>. Func<T> denotes a delegate which is pretty much a pointer to a method and Expression<Func<T>> denotes a tree data structure for a lambda expression. This tree structure describes what a lambda expression does rather than doing the actual thing. It basically holds data about the composition of expressions, variables, method calls, ... (for example it holds information such as this lambda is some constant + some parameter). You can use this description to convert it to an actual method (with Expression.Compile) or do other stuff (like the LINQ to SQL example) with it. The act of treating lambdas as anonymous methods and expression trees is purely a compile time thing.

                Func<int> myFunc = () => 10; // similar to: int myAnonMethod() { return 10; }
                

                ½«ÓÐЧµØ±àÒë³ÉÒ»¸öʲô¶¼²»µÃµ½²¢·µ»Ø 10 µÄ IL ·½·¨.

                will effectively compile to an IL method that gets nothing and returns 10.

                Expression<Func<int>> myExpression = () => 10;
                

                ½«±»×ª»»ÎªÃèÊöÒ»¸öûÓвÎÊý²¢·µ»ØÖµ10µÄ±í´ïʽµÄÊý¾Ý½á¹¹:

                will be converted to a data structure that describes an expression that gets no parameters and returns the value 10:

                ËäÈ»ËüÃÇÔÚ±àÒëʱ¿´ÆðÀ´Ïàͬ£¬µ«±àÒëÆ÷Éú³ÉµÄÈ´ÍêÈ«²»Í¬.

                While they both look the same at compile time, what the compiler generates is totally different.

                Õâƪ¹ØÓÚΪʲôҪʹÓñí´ïʽ&lt;Func&lt;T&gt;&gt;¶ø²»ÊÇ Func T ?µÄÎÄÕ¾ͽéÉܵ½ÕâÁË£¬Ï£ÍûÎÒÃÇÍƼöµÄ´ð°¸¶Ô´ó¼ÒÓÐËù°ïÖú£¬Ò²Ï£Íû´ó¼Ò¶à¶àÖ§³Ö¸ú°æÍø£¡

                ±¾Õ¾²¿·ÖÄÚÈÝÀ´Ô´»¥ÁªÍø,Èç¹ûÓÐͼƬ»òÕßÄÚÈÝÇÖ·¸ÁËÄúµÄȨÒ棬ÇëÁªÏµÎÒÃÇ£¬ÎÒÃÇ»áÔÚÈ·ÈϺóµÚһʱ¼ä½øÐÐɾ³ý£¡

                Ïà¹ØÎĵµÍƼö

                Multicast delegate weird behavior in C#?(C# ÖеĶಥίÍÐÆæ¹ÖÐÐΪ?)
                Parameter count mismatch with Invoke?(²ÎÊý¼ÆÊýÓëµ÷Óò»Æ¥Åä?)
                How to store delegates in a List(ÈçºÎ½«´ú±í´æ´¢ÔÚÁбíÖÐ)
                How delegates work (in the background)?(´ú±íÈçºÎ¹¤×÷(ÔÚºǫ́)?)
                C# Asynchronous call without EndInvoke?(ûÓÐ EndInvoke µÄ C# Òì²½µ÷ÓÃ?)
                Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() ºÍ·ºÐÍ:´íÎó°ó¶¨µ½Ä¿±ê·½·¨)

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

                      <small id='nWl5U'></small><noframes id='nWl5U'>

                      <legend id='nWl5U'><style id='nWl5U'><dir id='nWl5U'><q id='nWl5U'></q></dir></style></legend>
                          <tbody id='nWl5U'></tbody>
                      1. <tfoot id='nWl5U'></tfoot>