C # Programming Guide (I) lambda expressions

Source: Internet
Author: User

"Lambda expression" is an anonymous function and is a new feature introduced in C #3.0.

Lambda operator =>, which is read as "goes ".

 

 

 

The following code demonstrates the appearance of a Lambda expression:

  1            private delegate void PrintDelegate(string s);
2
3 //c#1.0 new delegate instance
4   PrintDelegate test1 = new PrintDelegate(Print);
5
6 //c#2.0 anonymous method
7   PrintDelegate test2 = delegate(string s) { Console.WriteLine("anonymous method:" + s); };
8
9 //c#3.0 lambda
10   PrintDelegate test3 = s => { Console.WriteLine("lambda:" + s); };
11
12 test1("Hello, World!");
13 test2("Hello, World!");
14 test3("Hello, World!");
15  

 

The same lambda expression also has a simple writing method and a complete writing method. The following four lambda expressions are actually the same function:

  1         private delegate int Times(int i);
2
3 Times test4 = x => x * x;
4 Times test5 = (x) => x * x;
5 Times test6 = (int x) => x * x;
6 Times test7 = (int x) => { return x * x; };
7
8 Console.WriteLine(test4(10));
9 Console.WriteLine(test5(10));
10 Console.WriteLine(test6(10));
11 Console.WriteLine(test7(10));

 

 

 

The first method is the simplest.

The second method is to have an input parameter, but the type is inferred by the compiler.

There is a clear type of input parameters.

The fourth method is to convert a lambda expression into a lambda statement (with a pair of braces ).

 

 

The following is an example with two parameters:

 1             private delegate int ThreeTimes(int x, int y);
2
3 ThreeTimes test8 = (x, y) => x * y;
4 ThreeTimes test9 = (int x, int y) => x * y;
5 ThreeTimes test10 = (int x, int y) => { return x * y; };
6
7 Console.WriteLine(test8(3,4));
8 Console.WriteLine(test9(3,4));
9 Console.WriteLine(test10(3,4));

 

Two or more parameters cannot be written in a simple way, and must be declared in parentheses. The example is simple. You can see it at a glance ..

 

 

The following is a lambda expression without parameters:

 1            private delegate void Void();       
2
3 Void test11 = () => { Console.WriteLine("Void Params"); };
4 test11();

 

 

 

 

The key to introducing Lambda expressions is the new Linq features introduced by c #3.0. Method-based query. The following is the simplest example code: (remember to reference the Linq namespace because the extension method of the LINQ is required)

            
Int [] testNumber = new [] {1, 2, 3, 4, 5 };
// Returns the number of even numbers.
Int test14 = testNumber. Count <int> (x => x % 2 = 0 );

 

Download Sample Code: Download

 

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.