ABCD
ABCD

Reputation: 907

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?

Upvotes: 7

Views: 654

Answers (2)

Habib
Habib

Reputation: 223277

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

Upvotes: 3

Andrew Cooper
Andrew Cooper

Reputation: 32576

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); })

Upvotes: 7

Related Questions