Let’s take a look at two codes:
Copy the codeThe code is as follows:
Thread t = new Thread(() =>
{
AddIt AddDelegate = new AddIt(AddItem);
(AddDelegate);
});
Thread t3 = new Thread(new ThreadStart(() =>
{
AddIt AddDelegate = new AddIt(AddItem);
(AddDelegate);
}));
Both of these writing methods are OK, and the IL comes out is the same, but the meaning is different. The latter is equivalent to anonymous method, the former
It's more like an "anonymous" commission, actually written like this:
Copy the codeThe code is as follows:
Thread t2 = new Thread(() =>
{
(new AddIt(() => { this.("bbb"); }));
});
This has the same effect as the above two writing methods. When you first come into contact with it, you may be a little confused like me. How should you use Lamdba expressions?
First of all, we need to clarify several characteristics of Lamdba expression:
1. The Lamdba expression is a delegate type:
Copy the codeThe code is as follows:
MethodInvoker invoker = () => { (); };
//is actually equal to the following form.
MethodInvoker invoker = delegate() { (); };
//This is the usage of anonymous delegate for Lamdba expressions
2. Lamdba expressions can be used as anonymous method
Copy the codeThe code is as follows:
MethodInvoker invoker = new MethodInvoker(() => { (); });
//Equivalent to:
MethodInvoker invoker = new MethodInvoker(MyFunc);
partial void MyFunc()
{
();
}
We know that when constructing a new delegate, its constructor needs a function pointer as a parameter, which is the delegate type
In fact, when we go to new a delegation like this:
Copy the codeThe code is as follows:
MethodInvoker invoker = new MethodInvoker(MyFunc);
"MyFunc" is a delegate, not just a function name. The compiler will generate an anonymous delegate for this constructor:
Copy the codeThe code is as follows:
MethodInvoker invoker = new MethodInvoker(delegate() { MyFunc(); });
You can understand the behavior of the compiler in this way: Anonymous methods are actually delegates.
So why can we write this:
Copy the codeThe code is as follows:
MethodInvoker invoker = () => { (); };
Let's take a look:
First, the delegate constructor requires a delegate parameter, so we usually have to write it like this:
Copy the codeThe code is as follows:
MethodInvoker invoker = new MethodInvoker(delegate() { MyFunc(); });
But anonymous delegate can be converted into function pointers (void() target):
So it is OK to write this way:
Copy the codeThe code is as follows:
MethodInvoker invoker = new MethodInvoker(MyFunc);
//An anonymous delegate can also be described as a Lambda expression
MethodInvoker invoker = new MethodInvoker(() => { (); });
//On the other hand, due to the compiler's support for "loose delegation", MethodInvoker class delegation can be converted into anonymous class delegation:
Copy the codeThe code is as follows:
MethodInvoker invoker = delegate() { (); };
//Anonymous delegates can be described by Lambda expressions
MethodInvoker invoker = () => { (); };
From the above, we can see how a complex delegate is translated into a concise Lambda expression. This is undoubtedly another manifestation of improving productivity for programmers!