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