25

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?

4 Answers 4

20

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

4
  • 6
    _ does have a special meaning as of C# 7. Not for lambda expression parameter names (as far as I can tell) but definitely for other things, such as pattern matching, deconstruction, out variables and even regular assignments. (For example, you can use _ = 5; without declaring _.) I realize this answer was written several years before C# 7, but given that the last line has become incorrect, it would be good to edit this if you have time.
    – Jon Skeet
    Sep 9, 2017 at 15:58
  • 1
    @JonSkeet Thanks for noticing, I added your comments in an edit. I need to get better at predicting the future. Sep 11, 2017 at 7:52
  • I'm often in the same position - in this case I hadn't actually noticed that it was an old answer for quite a while... the new answer had put the question on the front page.
    – Jon Skeet
    Sep 11, 2017 at 7:55
  • So to summarize, the expression new [] { 1, 2, 3 }.Select(_ => DoStuff(_)) is syntactically correct but semantically wrong? I see this all over the place and just found out about the discard pattern. To me it is seems practical to not having to clutter a tiny lambda with characters #firstworldproblems Jan 25, 2019 at 9:47
9

The short answer is: no, you have to name every parameter, and the names have to be unique.

You can use _ as one parameter name because it is a valid identifier in C#.
However, you can only use it once.

2
5

Now You Can!

From C# 9.0 onwards, you can use the underscore character to "discard" one or more lambda parameters. From Microsoft Docs:

Beginning with C# 9.0, you can use discards to specify two or more input parameters of a lambda expression that aren't used in the expression:

Func<int, int, int> constant = (_, _) => 42;
1

In C# 7, you can use discards. Discards are write only variables which you can not read. It is basically for variables that you do not wish to use more details here

4
  • 6
    This is true and has been missed by other answers, but isn't relevant in the case of lambda expression parameters as far as I can tell. (You can't use (_, _, x) => x for example.)
    – Jon Skeet
    Sep 9, 2017 at 15:59
  • @JonSkeet so you cannot use this? (_, foo) => bar(foo) Sep 19, 2017 at 10:36
  • 2
    You can, but then _ is just a regular parameter. You can't use (_, _, x) because then you'd be declaring the same parameter twice. (Whereas you can use _ multiple times in patterns.)
    – Jon Skeet
    Sep 19, 2017 at 10:53
  • @JonSkeet Ok, now I get it Sep 20, 2017 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.