views:

927

answers:

5

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.

+1  A: 

The () => ... syntax explicitly specifies that the lambda takes no parameters. Perhaps the language could be modified so that () => really meant "Infer the parameters of this lambda for me" in the same way the delegate syntax does, but that would make the language more complicated. When designing new language features, you start at minus 100, and I don't think this one passes the test.

There may also be more technical reasons why this would be difficult to implement (which is probably more in line with what you were asking for, but I doubt the technical reasons drove this decision if it ever came up).

Daniel Plaisted
A: 

I'd say it's to have a forced use of the parameters of the lambda expression.

Take your first example, how would you interact with the passed in value, there's no local representation of it.

Slace
+10  A: 

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.

tvanfosson
Whether this is right or not depends on how you read the answer. The generated IL is *definitely* for only a method taking an int. The compiler *has* to know the compile-time type it's trying to convert it to - if you change the signature of action to just "Delegate" you'll get a compile-time error.
Jon Skeet
What I meant (and thought I said) was by omitting the parameter list from the delegate in the first sample, the compiler "fills it in" when creating the method so that it does match the required signature. Lambda expressions don't allow you to omit the parameter list -- an empty list infers a method with no parameters -- and so causes a compile-time error.
tvanfosson
+5  A: 

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.

it depends
+1  A: 

As others said, no, you can't skip declaring the parameters to a lambda. But, for cleanliness, I suggest giving them a name such as _. For example

foo.Click += (_,__) => { ... }

You aren't ignoring them per-se, but you're indicating you don't care what they are and will not use them.

MichaelGG
and will confuse the hell out of anyone new to the lambdas, or not familiar with your convention.
frou
Anyone new to lambdas needs training before they can work on any of our projects anyways. That's a hiring/training/HR issue. I see no benefit in writing to people who'll get confused by simple stuff :).
MichaelGG