Recently, I used lambda expressions in the Code for modifying a project. By the way, I learned that lambda must be clear about the anonymous method and the Delegate method, make sure that Delegate understands the pointer of C ++. If you have used Delegate before, let's share the test code.
1. Delegate
[Csharp]
<Span style = "font-size: 18px;"> public delegate void MyDelegate (string message );
Public class TestClass
{
Public void HelloDelegate (string message, MyDelegate md)
{
If (md! = Null)
{
Md (message );
}
}
}
Private void button4_Click (object sender, EventArgs e)
{
TestClass tc = new TestClass ();
Tc. HelloDelegate ("hello delegate", HelloDelegate );
}
Public void HelloDelegate (string message)
{
MessageBox. Show (message );
}
</Span>
2. Anonymous method:
[Csharp]
<Span style = "font-size: 18px;"> TestClass tc = new TestClass ();
Tc. HelloDelegate ("hello anonymous method", delegate (string name)
{
MessageBox. Show (name );
}); </Span>
3. Lambda expressions:
[Csharp] px; "> TestClass tc = new TestClass ();
Tc. HelloDelegate ("hello lambda", n => MessageBox. Show (n); </span>
In fact, by comparing the code, we can see that the anonymous method only simplifies proxy writing, while lambda only simplifies the writing of an anonymous method. Of course, this is just an application of lambda, lambda is also very convenient in terms of collection, which will be shared with you later.
When I wrote the test code, I found that the new features of C #, such as lambda, anonymous method, implicit type, and object initiator, are similar to those of javascript syntax. I searched online articles, this is true.
Reference the original article:
C # Historical Review
Looking back at the development history of C #, C #1.0 completely imitates Java and retains some features of C/C ++, such as struct, Which is easy for new scholars to use; C #2.0 has added generics, which is exactly the same as Java 3.0's generics. C # has added a bunch of syntactic sugar and has introduced Linq without modifying the CLR, although many projects are not used for a variety of reasons such as performance, it is very suitable for the rapid development of small programs, reducing the workload of programmers and improving the readability of the Code; C #4.0 adds the Dynamic Language Feature, from which you can see the shadows of many dynamic languages such as javascript and python. Although the path from static language is becoming more and more different, these features also aim to improve the productivity of programmers. As for whether it is accepted or not, let's talk about time.