Simple Lambda expressions and simple lambda expressions

Source: Internet
Author: User

Simple Lambda expressions and simple lambda expressions

Lambda expressions are often used during development.

There are also many articles about lambda expressions in the garden, mostly from the horizontal perspective.

However, lambda expressions may not be clear about how they actually look like and what the form represents.

This article will start from the source, vertical analysis, has evolved our commonly used lambda expressions.

 

Introduction

For convenience, let's take an example to list a typical form of Lambda expressions:

X => x + 1

 

As you can see, the syntax of Lambda expressions consists of three parts:

1. Lambda operator => read as goes;

2. parameter x on the left side;

3. Lambda subject on the right

 

It can be seen that the so-called Lambda expression is an anonymous function, or a simple form of an anonymous function. Anonymous functions are assigned to the delegate type.

 

Suppose we declare a delegate:

Delegate int Mydel (int x );

 

Lambada expressions (anonymous functions) are used to fill delegate variables:

Mydel del = x => x + 1;

 

Let's start with this example.

 

Evolution Process

As I have just said, lambda expressions are short for anonymous methods. When it comes to lambda expressions, we need to talk about anonymous methods, while anonymous methods are the inline declaration methods during delegate initialization.

So let's start with the delegation and export the lambda expression Mydel del = x => x + 1.

 

Similar to a class, a delegate is a user-defined reference type.

A class represents a collection of data and methods, while a delegate holds one or more methods and a series of predefined operations.

 

Delegate can be analogous to a class. The process of creation and use is as follows:

1. Declaration type

2. Declare type variables

3. Fill Variables

4. Use Variables

 

A complete example of the four steps:

Delegate int Mydel (int x );

Mydel del;

Del = new Mydel (xxx method); // You can also use the simplified del = xxx method;

Del (2 );

 

In step 3, initialize the delegate to use the xxx method.

If the method is used to initialize the delegate, it will only be used once. In this case, apart from the syntax required to create the delegate, there is no need to create an independent named method, which leads to the anonymous method.

The anonymous method is an inline declaration method when the delegate is initialized. Examples of non-anonymous methods are as follows:

Public static int Add1 (int x)

{

Return x + 1;

}

Delegate int MyDel (int x );

Call:

MyDel del = Add1;

Del (2 );

 

The complete example is transformed into an anonymous method:

Delegte int MyDel (int x );

Call:

MyDel del = delegate (int x) {return x + 1 ;}

Del (2 );

 

Extract the syntax of the anonymous method from the example:

Delegate (Parameters) {ImplementationCode}

Notes:

1. Return Value: the declared return value is not displayed, which is the same as the delegate.

2. Parameters

In the following cases, you can use parentheses that are empty or omit parentheses to simplify the parameter list.

A. do not include any out parameter B. Do not use any parameter

The parameter list can be omitted if the preceding two conditions are met. For example:

Delegate int SomeDel ();

SomeDel sDel = delegate {return 1 ;};

3. params Parameters

If the parameter list declared by the delegate contains the params parameter (Variable Number Parameter), the parameter list of the anonymous method ignores the params keyword.

Delegate void SomeDel (int x, params int [] y );

SomeDel sDel = delegate (int x, int [] y ){... }

 

The anonymous method is ready. After a slight change, we can get the Lambda expression.

 

Take MyDel del = delegate (int x) {return x + 1 ;}as an example.

Conversion steps:

1. Delete the delegate keyword and place =>

MyDel del = (int x) =>{ return x + 1 ;};

Can be further simplified

2. Remove the parameter type

MyDel del2 = (x) =>{ return x + 1 ;};

Note:

The compiler can know the type of the delegate parameter from the delegate Declaration, so the type can be omitted.

A list of parameters with types is called an explicit type.

The parameter list of the omitted type is called implicit type.

3. Remove the parameter scratch number.

MyDel del3 = x =>{ return x + 1 ;};

Note:

If there is only one implicit type parameter, the scratch mark can be omitted.

4. Remove the large scratch number and return of the subject.

MyDel del4 = x => x + 1;

Note:

Lambda expressions allow the body of expressions to be statement blocks or expressions.

If the statement block contains a return statement, you can replace the statement block with the expression after return.

 

In this way, we can get the form at the beginning of our article.

 

Summary

C #2.0 introduces the anonymous method, and C #3.0 introduces the lambda expression. As you can see, the lambda expression simplifies the use of anonymous functions and simplifies the code.

After understanding this article, I believe that everyone will have a general understanding of lambda expressions, and the idea will be clear when learning the specific usage.

Wish learning progress ~

 

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.