Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of the underscore in WaitCallback

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

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

like image 616
ABCD Avatar asked Oct 05 '12 04:10

ABCD


2 Answers

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); })
like image 78
Andrew Cooper Avatar answered Oct 30 '22 03:10

Andrew Cooper


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

like image 23
Habib Avatar answered Oct 30 '22 04:10

Habib