[C # foundation]c# lambda Expression of the evil complement

Source: Internet
Author: User

Today, looking at someone else's code, I found this--"= =", looks like a pointer in C, and like this expression-":)", no matter what, it really baffled me, so decided to study.


To put it simply, lambda expressions are like anonymous delegates.

Using unityengine;using system.collections;//to create a lambda expression, you need to specify an input parameter (if any) on the left side of the lambda operator,//And then enter an expression or a block of statements on the other side. Parentheses are optional only if the lambda has only one input parameter, otherwise parentheses are required.    Two or more input parameters within parentheses are separated by commas with the public class testcsharp:monobehaviour{delegate void Testdelegate ();    delegate void TestDelegate2 (string s);    delegate void TestDelegate3 (string m,string n);        delegate int TestDelegate4 (int i);//Use the This for Initializationvoid Start () {//=> to the right is a statement block that can contain any number of statements Testdelegate Testdela = () = {print ("Start test! "); Print ("Are You Ready?"        "); };        TestDelegate2 Testdelb = (x) + = {print (x);};        TestDelegate3 TESTDELC = (x, y) + = {print (× + y);};        Testdela ();        Testdelb ("HelloWorld");        TESTDELC ("World", "Hello");        + = right is an expression TestDelegate4 Testdeld = x = x * 11; Int J = Testdeld (8);        j = Print (j);        = = Right is the method testdelegate Testdele = () = TestA ();        Testdele (); Testdelegate testdelf = () = TESTB ();        Testdelf + = () = TESTC ();        Testdelf ();    TestD (() = Testb (), () = TESTC ()); void TestA () {print ("Good-bye!")    "); } void Testb () {print ("Ah!    "); } void Testc () {print ("I'm back!")    ");        } void TestD (testdelegate a,testdelegate b) {a ();    b (); } }


Operation Result:


In the Testd method, the lambda expression directly passes the argument to a delegate, you know.

There is also the MSDN on the explanation is quite detailed, click here to Teleport.

[C # foundation]c# lambda Expression of the evil complement

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.