Why can I use a lambda expression instead of a callback delegate?

I discovered a new C # syntax and don't understand what that means. Here is the syntax related code:

one)

BeginInvoke(new Action(() => { PopulateUI(ds); })); 

2)

 private void OnFormLoad() { ThreadPool.QueueUserWorkItem(() => GetSqlData()); } 

What is the meaning of new Action() and what is the meaning of the symbol => ?

The syntax for ThreadPool.QueueUserWorkItem was ThreadPool.QueueUserWorkItem(new WaitCallback(PrintOut), "Hello"); but here it shows ThreadPool.QueueUserWorkItem(() => GetSqlData()); so how does it work? Why is WaitCallback missing? Please explain in detail.

Many thanks.

+7
source share
4 answers

Take a look

Action delegate

Encapsulates a method that has a single parameter and does not return a value. You can use the delegate action to pass a method as a parameter without explicitly declaring a custom delegate.

and

and Lambda Expressions (C # Programming Guide)

A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.

All lambda expressions use the lambda operator =>, which is read as "going to." The left side of the lambda operator sets the input parameters (if any) and the right side contains the expression or statement block. The lambda expression x => x * x reads "x goes x times x".

+6
source

As others have said, this is a lambda, which is basically an anonymous (unnamed) local function.

This might make a little more difference if you look at some similar code that doesn't use lambdas:

 // With a lambda private void OnFormLoad() { ThreadPool.QueueUserWorkItem(() => GetSqlData()); } // Without a lambda private void OnFormLoad() { ThreadPool.QueueUserWorkItem(ExecuteGetSqlData); } private void ExecuteGetSqlData() { // If GetSqlData returns something, change this to "return GetSqlData();" GetSqlData(); } 

As with other code, usually you do not need to do a new Action . The problem is that the BeginInvoke method accepts a Delegate , which is a kind of old school, and breaks how most new codes work.

With newer code (which accepts something like Action or a specific type of delegate, such as WaitCallback ), you either write a lambda, or simply specify the name of the function inside your class. The code sample I wrote above demonstrates both of these options.

Also note that if you see something like: (Action) (() => Blah()) , this is almost the same as new Action(() => Blah()) .

+5
source

They are known as lambda expressions , which are not very different from delegates in C #.

An empty () means that there are no arguments, and between (optionally) {} there are bodies of the lambda expression. The => operator simply binds both expressions together to make a lambda expression. Aside, they are usually found in LINQ code.

There is nothing special about new Action() , just that it is a delegate that can display a lambda expression for itself.

As for ThreadPool.QueueUserWorkItem() , the WaitCallback argument is a delegate. You can pass the name of the named delegate as an argument, pass an anonymous delegate object, or write a lambda expression for that anonymous delegate (in your case, this is () => GetSqlData() ).

+4
source
+2
source

All Articles