Note that there are some explanatory texts on larger screens.

plurals
  1. POFunc Delegate vs Function
    text
    copied!<p>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.</p> <p>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</p> <pre><code>class Program { static void Main(string[] args) { Console.WriteLine(SayTwoWords("Hello", "World")); Console.ReadKey(); } // Option A private static Func&lt;string, string, string&gt; SayTwoWords = (a, b) =&gt; String.Format("{0} {1}", a, b); // Option B private static string SayTwoWords(string a, string b) { return String.Format("{0} {1}", a, b); } } </code></pre> <p>************EDIT************</p> <p>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:</p> <pre><code>public static class clsCompiledQuery { public static Func&lt;DataContext, string, IQueryable&lt;clsCustomerEntity&gt;&gt; getCustomers = CompiledQuery.Compile((DataContext db, string strCustCode) =&gt; from objCustomer in db.GetTable&lt;clsCustomerEntity&gt;() where objCustomer.CustomerCode == strCustCode select objCustomer); } </code></pre> <p>Is there any advantage to writing a function in this way?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload