Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delegates, Lambdas, Action, Func, Anonymous Functions

I just want to verify my understanding about the following

  • Delegate - a method signature
  • Lambdas - anonymous function
  • Anonymous Function - just that
  • Action - An anonymous function that returns nothing
  • Func - An anonymous function that returns something

hmm... they all do similar things, how do you define & know when to use each?

sorry, I don't explain well

like image 970
Jiew Meng Avatar asked Nov 25 '10 09:11

Jiew Meng


1 Answers

  • Delegate - it is not a method signature. It is a type which encapsulates a method. Hence a delegate declaration should have a signature similar to the method it wants to encapsulate. When to use Delegate - whenever you want to pass a method to another function. For more see this and this.

  • Lambdas - short hand and more expressive way of writing an anonymous function. But there is more to it. A lambda expression can also be converted to an expression tree. For more see this.

  • Anonymous Function - yes .. just that

  • Action - It is a delegate which can encapsulate a function that returns nothing. So you should think of it as a type that can encapsulate an action and use it when you need to pass an action around.

  • Func - A delegate that can encapsulate a function that returns something. But you should look at it as a type that can encapsulate a transformation and use when you want to pass around a transformation.

like image 128
Unmesh Kondolikar Avatar answered Sep 23 '22 19:09

Unmesh Kondolikar