31

Reading through C# in a Nutshell I noticed this bit of code that I've never came across:

_uiSyncContent.Post(_ => txtMessage.Text += "Test");

What is that underscore followed by an arrow? I've seen Lambda expressions written in a similar way but nothing with an underscore.

James Jeffery
  • 12,093
  • 19
  • 74
  • 108
  • 7
    mostly to indicate it is not used in lambda... – I4V Aug 18 '13 at 15:25
  • The above code actually fails because .Post expects 2 arguments, no one. Not sure why it's printed like that in the book though. – James Jeffery Aug 18 '13 at 15:33
  • 3
    I think question duplicates with: [`C# style: Lambdas, _ => or x =>?`](http://stackoverflow.com/questions/10538924/c-sharp-style-lambdas-or-x) – Grijesh Chauhan Aug 18 '13 at 18:16
  • Although a javascript question [here](https://stackoverflow.com/q/41085189/465053) but closely related – RBT Sep 15 '17 at 02:14
  • Possible duplicate of [C# style: Lambdas, \_ => or x =>?](https://stackoverflow.com/questions/10538924/c-sharp-style-lambdas-or-x) – Pac0 Jul 27 '18 at 09:59

1 Answers1

32

It's just a lambda expression that uses _ instead of x for its parameter. _ is a valid identifier so it can be used as a parameter name.

As mentioned in the comments, it's a convention among some developers to call it _ to indicate that it's not actually used by the lambda expression, but it's no more than that: a convention.

Note that this is not the same thing as a discard (introduced several years after this answer), which is a special variable for assigning values that aren't going to be used and will instead be discarded. Unlike discarded values, _ parameters continue to exist in lambda scope; they just aren't used anywhere in the lambda expression. And there can only be one _ in scope at a time.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • Thanks. I feel so embarrassed for not even picking that up. – James Jeffery Aug 18 '13 at 15:24
  • 6
    _Generally_ speaking, some developers use it to denote an argument that they don't care about since they _must_ assign arguments based on the delegate signature. – Chris Sinclair Aug 18 '13 at 15:25
  • 5
    so the only possible reason for doing this confusing syntax is to make it more CLEAR tha you are not going to be using the parameter in the lambda expression as in the example? – Andyz Smith Aug 18 '13 at 15:25
  • 2
    @ChrisSinclair very interesting. Threw me off a little. – James Jeffery Aug 18 '13 at 15:27
  • Thank you thank you. It can hard to explain illogical things using logic. – Andyz Smith Aug 18 '13 at 15:27
  • 2
    +1 just out of curiousity, and if there were 2 unused parameters? In that "school of thought", what would be the currect way to show that they aren't used? _1 and _2? – xanatos Aug 18 '13 at 15:29
  • @I4V beat me to it. I was just guessing. – James Jeffery Aug 18 '13 at 15:31
  • Btw, if you find the underscore use in C# confusing: [compare with Scala](http://stackoverflow.com/questions/8000903/what-are-all-the-uses-of-an-underscore-in-scala). – rsenna Aug 18 '13 at 15:36
  • I certainly don't know any scala but I have a feeling that languages that allow and encourage this kind of anonymous definition need more structure. Part of the coding process is for people to be able to read the code later. I think good programs and programmers will try to reduce the ambiguity, and actually design, like , a concrete reuable class hierarchy to do these kinds of things instead of relying on sytacticsugar and anonymous on the fly typing. Then when I'm reading, i KNOW what types are being returned , expected, conformed to, etc. – Andyz Smith Aug 18 '13 at 15:42
  • @AndyzSmith: Yes, it's important to know what types are being returned, expected, conformed to, etc.; but why is it important to know what types are being *ignored*? And anyway, `_` is an unused variable that is *required by the syntax*; it's hardly an argument that the language needs more structure, since the `_` is an artifact of the meaningless structure that it already requires. (A truly less-structured language would, I dunno, let you just drop the `_` when you don't need it, like [null-subject (human) languages](http://en.wikipedia.org/wiki/Null-subject_language).) – ruakh Aug 18 '13 at 18:13
  • Note that if there are very many parameters, none of which are used, an old-school C# 2.0 anonymous method may be better since you can let the compiler infer the number of parameters in that case. So for example `(x, y, z, u, v) => { DoIt(); }` can also be written `delegate { DoIt(); }`. – Jeppe Stig Nielsen Aug 18 '13 at 20:56