is it possible, to discard some arguments in lambda expressions by don't give them a name? E.g. I have to pass a Action<int,int>, but I'm only interested in the second param, i want to write something like
(_, foo) => bar(foo) // or (, foo) => bar(foo)
In the first case it is working. But the first parameter isn't really unnamed, because it has the name "_". So it isn't working, when I want to discard two or more. I choose _ because in prolog it has the meaning "any value".
So. Is there any special character or expression for my use case?
No, you can't. Looking at the C# language specification grammar, there are two ways to declare lambdas: explicit and implicit. Neither one allows you to skip the identifier of the parameter or to reuse identifiers (names).
explicit-anonymous-function-parameter: anonymous-function-parameter-modifieropt type identifier implicit-anonymous-function-parameter: identifier
It's the same as for unused function parameters in ordinary functions. They have to be given a name.
Of course you can use _
as the name for one of the parameters, as it is a valid C# name, but it doesn't mean anything special.
As of C# 7, _
does have a special meaning. Not for lambda expression parameter names but definitely for other things, such as pattern matching, deconstruction, out variables and even regular assignments. (For example, you can use _
= 5; without declaring _
.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With