anonymous function lamb expression in C #
Example one: (In fact, this is a bit of syntactic sugar)
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceconsoleapplication2{//anonymous functions in C #//Declaration of a Commission Delegate voidDel (); classProgram {Static voidShow () {Console.WriteLine ("show ..."); } Static voidTest () {Del D=NewDel (show); D (); } Static voidTest2 () {//You can write this ....Del d =Delegate() {Console.WriteLine ("show ..."); }; D (); //you can write it like that.Del D1 =Delegate{Console.WriteLine ("show ... .."); }; D1 (); //You can also write this, this is lamb expression;Del D2 = () ={Console.WriteLine ("show ... .."); }; D2 (); } Static voidMain (string[] args) {Test2 (); Console.ReadLine (); } }}
Lamb Expressions with parameters:
Static voidTest () {//You can write this .Dele d =Delegate(intj) {Console.WriteLine (j); }; D ( A); Dele D1= (j) = ={Console.WriteLine (j);}; D1 ( A); //This is the lamb expression that has the parameter; } Static voidMain (string[] args) {Test (); Console.ReadLine (); }
By the way, action Func predicate in C #;
Delegate at least 0 parameters, up to 32 parameters, can have no return value, or you can specify a return value type.
Action is a generic delegate with no return value.
The Action represents a delegate without parameters and no return value
Action<int,string> represents a delegate with an incoming parameter int,string no return value
Action<int,string,bool> represents a delegate with an incoming parameter Int,string,bool no return value
Action<int,int,int,int> represents a delegate that has passed in 4 int parameters, no return value
Action at least 0 parameters, up to 16 parameters, no return value.
Func is a generic delegate with a return value
Func<int> represents no parameter and returns a delegate with a value of int
Func<object,string,int> indicates that an incoming parameter is an object, and a string returns a delegate with a value of int
Func<object,string,int> indicates that an incoming parameter is an object, and a string returns a delegate with a value of int
Func<t1,t2,,t3,int> represents a delegate with an incoming parameter of T1,T2,,T3 (generic) that returns a value of int
Func at least 0 parameters, up to 16 parameters, are returned based on the return value generic. Must have a return value, not void
(4) predicate
predicate is a generic delegate that returns a bool type
Predicate<int> represents a delegate with an incoming parameter of int returning BOOL
predicate has and has only one parameter, the return value is fixed to bool
Instance:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Threading;namespaceconsoleapplication2{Delegate voidDele (inti); classProgram {Static voidAddintIintj) {Console.WriteLine (i+j); } Static intSubintIintj) {returnIJ; } Static BOOLIsTrue (inti) {if(I >0) return true; Else return false; } Static voidTest () {List<Action<int,int>> list =Newlist<action<int,int>>(); List. Add (add); //Call mode onelist[0]( -,Ten); //the last parameter is the return value;func<int,int,int> func=Sub; intResult=func ( -,Ten); Console.WriteLine (result); predicate<int> P =isTrue; BOOLRe = P ( -); Console.WriteLine (re); } Static voidTest2 () {//of course, we can write the drip in a different way .action<int,int> action =Delegate(intIintj) {Console.WriteLine (i+j); }; Action ( -,Ten); Func<int,int,int> func =Delegate(intIintj) {returnIJ; }; intresult = Func ( -,Ten); predicate<int> Pre =Delegate(inti) {BOOLRe=I==0?true:false; returnre; }; } Static voidTest3 () {//of course, we can also write that drip .action<int,int> action = (i, j) + = {Console.WriteLine (i +j); }; Action ( -,Ten); Func<int,int,int> func= (i,j) =>{returnI-J;}; intResult=func ( -,Ten); predicate<int> Pre = (i) = = {if(i = =0)return true;Else return false; }; BOOLval = Pre ( -); } Static voidTest4 () {thread thread=NewThread (Delegate() {Console.WriteLine ("Hahhahah ...."); }); Thread. Start (); Thread thread2=NewThread (() ={Console.WriteLine ("hahah ..."); }); Thread2. Start (); } Static voidMain (string[] args) { //in fact, it is equivalent to this drop: public delegate void Action<t> (T arg);Test (); Console.ReadLine (); } }}
anonymous function lamb expression in C #