C#lambda expression Recording

Source: Internet
Author: User

Foundation, you are master, please float away ...

Usually do the project write code rarely use lambda expression, hereby add, record down.

1 What is a lambda expression?

MSDN statement: using a lambda expression, you can write a local function that can be passed as a parameter or returned as a function call value

At first glance, really a bit uncomfortable, Chinese did not learn!!! It's machine-translated, and it's a roar ...

Take an example.

Lambda expression x=>x*x input x, output x*x

If you have one of these methods:

1 public int getX2 (int x) {return x*x;}

Note that there is a difference, how can the expression be the same as the method?

c# method is passed as a parameter! Yes, that's the Commission. For example:

1 Delegate int del (int  i);    2 Static void Main (string[] args)3{4     del mydelegate = x = x * x;< /c15>5     int j = mydelegate (5//j =6 }

2 Lambda type

An expression lambda

The lambda expression that the expression is on the right side of the = = operator is called expression Lambda. The expression lambda returns the result of an expression and takes the following basic form:

(input parameters) = Expression

See Example:

Static void Main (string[] args)        {            int56;            Func<intint int > f = ((x1, y1) = x1 + y1);                      Console.WriteLine (f (x, y). ToString ());            Console.read ();         }

What's the result? 11

Func is a generic delegate that typically comes with a return value.

Statement Lambda

A statement lambda is similar to an expression lambda expression, except that the statement is enclosed in curly braces:

1 (input parameters) = {statement;}

Such as:

1  Static void Main (string[] args)2        {3             action<string> F = (x) = = {Console.WriteLine (x);}; 4             F ("hello,world"); 5             console.read (); 6         }

Where: Action is a non-return value, generic delegate that is provided internally by. Net.

The structure is as follows:

 1  delegate  void   Action ();  2  delegate  void  Action<in  t> (T obj);  3  delegate  void  Action<in  t, (T obj,t1 obj1);  4  delegate  void  Action<in  t,in  t2> (T obj,t1 obj1,t2 obj2); 
...

Just write it down here.

C#lambda expression Recording

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.