C # learning new features: Delegate, anonymous method, lambda expression

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.