Underscore Arrow (_ => ...) What Is This?
Asked Answered
A

1

32

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.

Anus answered 18/8, 2013 at 15:23 Comment(5)
mostly to indicate it is not used in lambda...Interpolation
The above code actually fails because .Post expects 2 arguments, no one. Not sure why it's printed like that in the book though.Anus
I think question duplicates with: C# style: Lambdas, _ => or x =>?Aparri
Although a javascript question here but closely relatedGourmont
Possible duplicate of C# style: Lambdas, _ => or x =>?Karonkaross
P
33

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.

Philps answered 18/8, 2013 at 15:24 Comment(11)
Thanks. I feel so embarrassed for not even picking that up.Anus
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.Downey
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?Oraorabel
@ChrisSinclair very interesting. Threw me off a little.Anus
Thank you thank you. It can hard to explain illogical things using logic.Oraorabel
+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?Hogarth
@Interpolation beat me to it. I was just guessing.Anus
Btw, if you find the underscore use in C# confusing: compare with Scala.Accumulator
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.Oraorabel
@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.)Neurocoele
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(); }.Psychographer

© 2022 - 2024 — McMap. All rights reserved.