Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
380 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...