(_) => DoWork (); How is underscore valid as an anonymous delegate parameter?

In a great answer about starting the timer immediately , I could see the following code:

timer.Elapsed += timer_Elapsed; ThreadPool.QueueUserWorkItem((_) => DoWork()); ... void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { DoWork(); } void DoWork() { // etc... } 

I tried this myself and I came across this line where, as I thought, there was a typo in the delegate's anonymous construct:

  What? | V ThreadPool.QueueUserWorkItem((_) => DoWork()); 

What hidden rule makes the underscore "_" acceptable as the parameter name in anonymous deletes?

+6
source share
1 answer

The underlined character is a regular identifier in C #. For example, my_money . So _ fair as x .

You can also write _ => DoWork() , which I think is more common.

+10
source

Source: https://habr.com/ru/post/982089/


All Articles