Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I ignore delegate parameters with lambda syntax?

I am curious why C# allows me to ignore delegate parameters in some cases but not others.

For instance this is permitted:

Action<int> action = delegate { Console.WriteLine("delegate"); }; 

but this is not:

Action<int> action = () => Console.WriteLine("lambda"); 

Is there a way to initialize a delegate and ignore the parameters using a lambda? I know that I can add a single parameter to the lambda and fix the previous line but this is more of an academic question pertaining to the compiler and why or how this works.

like image 737
Andrew Hare Avatar asked Feb 03 '09 02:02

Andrew Hare


People also ask

Is lambda expression delegate?

Since a lambda expression is just another way of specifying a delegate, we should be able to rewrite the above sample to use a lambda expression instead of an anonymous delegate. In the preceding example, the lambda expression used is i => i % 2 == 0 . Again, it is just a convenient syntax for using delegates.

What is the difference between lambdas and delegates?

They are actually two very different things. "Delegate" is actually the name for a variable that holds a reference to a method or a lambda, and a lambda is a method without a permanent name. Lambdas are very much like other methods, except for a couple subtle differences.

How are an anonymous delegate and a lambda expression related?

Anonymous Method is an inline code that can be used wherever a delegate type is expected. Microsoft introduced Anonymous Methods in C# 2.0 somewhere around 2003. Lambda expression is an anonymous method that you can use to create delegates or expression tree types.

Can we pass delegate as parameter?

Because the instantiated delegate is an object, it can be passed as an argument, or assigned to a property. This allows a method to accept a delegate as a parameter, and call the delegate at some later time.


2 Answers

I believe that your first sample actually creates an anonymous function that is able to take on many different signatures whose body is the single statement Console.WriteLine.... Because it can match different signatures, it does not cause a problem. In the second sample, the lambda syntax itself defines a function that takes no parameters with the same body. Obviously the latter is not consistent with the defined Action so you get the error.

C# Anonymous Method Reference

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, and this means that an anonymous method can be converted to delegates with a variety of signatures. This is not possible with lambda expressions.

like image 100
tvanfosson Avatar answered Sep 22 '22 22:09

tvanfosson


To elaborate on tvanfosson's answer; this behavior is described in the C# 3.0 language specification (§7.14):

The behavior of lambda-expressions and anonymous-method-expressions is the same except for the following points:

• anonymous-method-expressions permit the parameter list to be omitted entirely, yielding convertibility to delegate types of any list of value parameters.

• lambda-expressions permit parameter types to be omitted and inferred whereas anonymous-method-expressions require parameter types to be explicitly stated.

• The body of a lambda-expression can be an expression or a statement block whereas the body of an anonymous-method-expression must be a statement block.

• Since only lambda-expressions can have an expression body, no anonymous-method-expression can be successfully converted to an expression tree type (§4.6).

I think:

Action<int> action = () => Console.WriteLine("lambda"); 

is the equivalent of:

Action<int> action = delegate() { Console.WriteLine("delegate"); }; 

which wouldn't compile either. As Daniel Plaisted says () is explicitly saying there aren't any parameters.

If there were an equivalent of delegate{} it might be:

Action<int> action = => Console.WriteLine("lambda") 

Which isn't very pretty and I suspect it suspect isn't in the spirit of lambda expressions.

like image 23
codybartfast Avatar answered Sep 22 '22 22:09

codybartfast