Domanda

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?

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top