Duplicate: delegate keyword vs. lambda notation
I understand the anonymous methods can be used to define delegates and write inline functions. Is using Lambda expressions any different from this?
I guess I am a little confused on when to use what.
Edit: Also, appears that to use either anonymous or lambdas, there needs to be an Extension method for the type?
Lambda Expression –A function without name is called anonymous function. For lambda expression we can say that it is anonymous function. Example: Kotlin.
In Python, an anonymous function is a function that is defined without a name. While normal functions are defined using the def keyword in Python, anonymous functions are defined using the lambda keyword. Hence, anonymous functions are also called lambda functions.
Anonymous method is a block of code, which is used as a parameter for the delegate. An anonymous method can be used anywhere. A delegate is used and is defined in line, without a method name with the optional parameters and a method body. The scope of the parameters of an anonymous method is the anonymous-method-block.
Anonymous MethodAnonymous methods in C# can be defined using the delegate keyword and can be assigned to a variable of delegate type.
A lambda expression is simply shortcut syntax for an anonymous method. Anonymous methods look like this:
delegate(params) {method body}
The equivalent lambda expression would look like this:
params => method body
In short, all lambda expressions are anonymous methods, but it is possible to have an anonymous method that is not written in lambda syntax (like the first example above). Hope this is helpful!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With