开发者

Why can I use a lambda expression in place of a callback delegate?

开发者 https://www.devze.com 2023-01-31 04:55 出处:网络
I 开发者_如何学运维discovered some new C# syntax and do not understand what it means. Here is the syntax-related code:

I 开发者_如何学运维discovered some new C# syntax and do not understand what it means. Here is the syntax-related code:

1)

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 of 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.

Thanks a lot.


Have a look at

Action Delegate

Encapsulates a method that has a single parameter and does not return a value. You can use the Action delegate 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 "goes to". The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x => x * x is read "x goes to x times x."


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

This might make a bit more sense 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 for the other code, normally you shouldn't have to do new Action. The problem is that the BeginInvoke method takes a Delegate, which is sort of old school, and breaks how most new code works.

With newer code (that takes something like Action, or a specific type of delegate, like WaitCallback), you either write a lambda, or simply give the name of a function inside your class. The example code I wrote above demonstrates both of these.

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


These are known as lambda expressions, which aren't very different from delegates in C#.

The empty () mean there are no arguments, and what's between the (optional) {} are the lambda expression bodies. The => operator simply associates both expressions together to make a lambda expression. As an aside, they're commonly found in LINQ code.

There's nothing special about new Action(), just that it's a delegate that can map a lambda expression to itself.

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


Lambda expression

Action Delegate

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号