Use of lambda expressions in "C #" C #

Source: Internet
Author: User

A lambda expression.
Lambda expressions are new to c#3.0 (Java is JDK8 to support), and it's not surprising if you've learned c#2.0 before. Here are some versions of the C # language and Visual Studio information:
C # 1.0 with Visual Studio.NET
C # 2.0 with Visual Studio 2005
C # 3.0 with Visual Studio 2008,2010 (. NET 2.0, 3.0, 3.5)
C # 4.0 with Visual Studio (. NET 4)
C # 5.0 with Visual Studio (. NET 4.5), (. NET 4.5.1, 4.5.2)
C # 6.0 with Visual Studio (. NET 4.6, 4.6.1, 4.6.2)
C # 7.0 with Visual Studio (. NET 4.6.2, 4.7)

Give you an example.
For example, I define a delegate:
delegate int Method (int a, int b);
Then define a method:
int Add (int a, int b)
{
return a + B;
}
I may need to invoke this method through a delegate:
Method m + = ADD;
Console.WriteLine (M (2, 3));
====================================================
But if you use a LAMBDA expression:
Method m + = (A, b) = + A + b;
Console.WriteLine (M (2, 3));
The definition of the method can be omitted.
In fact, a LAMBDA expression simply simplifies the syntax of an anonymous method.

anonymous method syntax with C # 2.0:
Method m + = delegate (int a, int b) {return a + B;};
Console.WriteLine (M (2, 3)); *******************************************************************************C # + = (s, e) = = What do these characters mean?Public MainWindow ()
{
InitializeComponent ();
This. Loaded + = (s, e) = Discoverkinectsensor ();
This. Unloaded + = (s, e) = = This.kinect = null;
In the above code, + = Adds a delegate to the delegate chain, (s,e) + = is a lambda expression that creates a delegate that is the subject of the delegate process, which is the part that follows.

In fact, this is the equivalent
This. Loaded + = new EventHandler (form_loaded);

private void Form_loaded (object sender, EventArgs e) {
Discoverkinectsensor ();
} original link: the role of = = in C #

Use of lambda expressions in "C #" C #

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.