Func vs Feature Delegate

Can someone tell me the benefits of using a delegate rather than calling the function itself, as shown below (or, in other words, why choose option A over option B)? I looked at someone linq code last night, and they had something similar to option A, but it was used to return the compiled linq request.

I understand that the first can now be transferred to other functions .. just not sure about its practicality. By the way, I understand that this will not compile as is .. uncommented one of the functions before publication. Tya

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 initially made me think about it:

 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 this way?

+11
c # delegates
Jun 24 '10 at 19:22
source share
6 answers

There is no advantage in the code you posted. In your code, using a delegate just adds complexity as well as extra execution cost - so you better just call the method directly.

However, delegates have many uses. Moving to other methods is the main use, although storing a function and using it later is also very useful.

LINQ is completely built on this concept. When you do:

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

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

+13
Jun 24 '10 at 19:25
source share

A very useful feature of delegates is that you can send them wherever you want. It looks like you need your function everywhere. A big advantage for this is event handling. Say you have a button, and when the user clicks this button, you need to call any number of functions. If you think about it, you can do several ways:

You could: Call a function that calls the function you want to call. This means that for every new function that you want to call, you must write it hard to that function. Very annoying.

OR You can have an open list of names of each function that you want to call (delegates), and everyone can add or remove these functions at any time if the owner of the click event does not need to know or even do any work regarding any of them When a click event occurs, each event in the list is called and dispatched with the same parameters, and you're done.

+4
Jun 25 '10 at 20:51
source share

This is only useful if you need to delegate. If you can solve this function at compile time, it is less useful.

+3
Jun 24 '10 at 19:25
source share

Using the static method, you must go through all the necessary variables.
With a delegate, you can embed your implementation and access variables in a scope.

+2
Jun 24 '10 at 19:26
source share

you can use delegates as "function pointers", so you can give functions or "actions" to other functions to execute.

what would also be interesting with delegates is the ability to “precompile”, for example, “create” a new function, and then return this function to your application

+1
Jun 24 '10 at 19:26
source share
 // 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); } 

In the case above, option B is what I would go with if I don't need the SayTwoWords functionality that needs to be changed. With option A, a different function may be assigned to SayTwoWords . Catch the more detailed differences in this answer :

There is a situation where option A makes sense. Consider the case where you must compile an expression for a delegate. Since compiling an expression is difficult, this is what you would like to do it only once. A similar pattern helps:

 public static class Cache<T> { public static readonly Func<T> Get = GetImpl(); static Func<T> GetImpl() { //build expression and return compiled delegate } } 

instead

 public static class Cache<T> { public static T Get() { //build expression, compile delegate and invoke the delegate } } 

In the first case, when you call Get , GetImpl is executed only once, where, as in the second case, the (expensive) Get will be called each time.

+1
Jun 29 '13 at 13:55 on
source share



All Articles