Underline value in WaitCallback

ThreadPool.QueueUserWorkItem(new WaitCallback((_) => { MyMethod(param1, Param2); }), null); 

Could you explain the meaning of the underscore (_) in the WaitCallBack constructor?

+7
source share
2 answers

The unserscore character is actually an argument to an anonymous method. This is a common technique if you need a lambda expression that takes an input parameter, but the input parameter is not actually used.

It is exactly equivalent:

 new WaitCallback(x => { MyMethod(param1, Param2); }) 
+6
source

Underscore is a valid C # identifier name and is commonly used with a lambda expression to specify an expression parameter that will be ignored

You can see: Nice C # idiom for parameterless lambdas

+2
source

All Articles