Can I ignore delegate parameters with lambda synta

2019-01-22 07:42发布

问题:

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:

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.



回答2:

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.



回答3:

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.



回答4:

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).



回答5:

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.



回答6:

What about this?

Func<int> lamdapointer = () => TwoArgMethodThatReturnsInt(10,20); // the same method cannot be called with the delegate "NoArgmethodThatReturnsInt"


lamdapointer();

Delegate int NoArgmethodThatReturnsInt();
NoArgmethodThatReturnsInt del = NoArgmethodThatReturnsInt; // only this is possible with delegates


public int TwoArgMethodThatReturnsInt(int x,int y)
{
return x + y;
}

public int NoArgmethodThatReturnsInt()
{
return 20;
}


回答7:

Actually, delegate {} does not specify any parameters and fits any delegate method signature - therefore it is permitted in your first construcion.

The Lambda expression () => ...; specifically states parameterless delegate, which contradicts the signature required by Action - a delegate with single parameter.

You may want to use one of the following options.

If you need the action to have a parameter, you can do it the next way ("_" is a legal character for identifier name).

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

Or you may want to use parameterless Action as follows:

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