ThreadPool.QueueUserWorkItem(new WaitCallback((_) => { MyMethod(param1, Param2); }), null);
Could you please explain the meaning of underscore (_) in the WaitCallBack constructor?
The unserscore is actually the argument to the anonymous method. It's a common technique if a lambda expression that takes an input parameter is needed, but the input parameter is not actually used.
It's exactly equivalent to:
new WaitCallback(x => { MyMethod(param1, Param2); })
Underscore is a valid C# identifier name, and usually used with lambda expression to specify a parameter for the expression which will be ignored
You may see: Nice C# idiom for parameterless lambdas
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With