Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does anonymous methods omit parameter list? [closed]

I was reading this in the MSDN documentation on Anonymous Methods (C# Programming Guide), but I do not understand the part about omitting the parameter list. It says:

There is one case in which an anonymous method provides functionality not found in lambda expressions. Anonymous methods enable you to omit the parameter list. This means that an anonymous method can be converted to delegates with a variety of signatures. This is not possible with lambda expressions.

Could you provide an example of ommiting a parameter list for an anonymous method?

like image 853
Damian Avatar asked Apr 07 '14 21:04

Damian


People also ask

What is the advantage of using anonymous methods?

The advantage of an anonymous function is that it does not have to be stored in a separate file. This can greatly simplify programs, as often calculations are very simple and the use of anonymous functions reduces the number of code files necessary for a program.

What will happen if you put a break statement inside the anonymous method?

break; cannot be used to exit methods, instead you need a return. And while inside a method your scope is limited to that method because it could have been called from anywhere. While inside the method there is no information on the calling scope and the code therefore does not know if there is a loop to break out of.

What is the difference between anonymous methods and lambda expressions?

Anonymous methods are basically functions without a name, with the ability to create closures. Lambda expressions are constructs that are convertible to both anonymous methods and expression trees, and follow more complex rules of type inference than anonymous methods.

What is the use of anonymous method in C#?

Anonymous methods provide a technique to pass a code block as a delegate parameter. Anonymous methods are the methods without a name, just the body. You need not specify the return type in an anonymous method; it is inferred from the return statement inside the method body.


1 Answers

I think you confused about lambda expressions and anonymous methods. What you need to understand is that lambda expressions are just syntantic sugars.For example, you can create an anonymous method that takes 2 integer parameter and returns an integer like this:

Func<int, int, int> func = delegate(int x, int y)
                           {
                                return x + y;
                           };

Using lambda syntax you can shorten that statement into this:

Func<int, int, int> func2 = (x,y) => x + y;

Also you don't really need to pass any argument to a lambda statement.For example this is completely valid:

Action act = () => Console.WriteLine("hello world");

So as a result, lambda expressions allows you to create anonymous methods with less code and they don't have any disadvantages as compared to anonymous methods because they are completely different things.You are comparing apples with oranges.

like image 123
Selman Genç Avatar answered Oct 22 '22 11:10

Selman Genç