Lambda expressions simply write anonymous methods in a simpler way, completely simplifying the pair. The use of the net delegate type.
Now, if we are going to use the FindAll () method of the generic list<>, you can use this method when you are extracting a subset from a collection.
// the only argument to the method is a generic delegate of type system.predicate<t> Public List<t> FindAll (predicate<t> match); // the delegate points to any method that takes a type parameter as a unique input parameter and returns a bool Public Delegate BOOL predicate< in t> (T obj);
When you call FindAll (), each item in,list<t> will pass in the method that the Predicate<t> object points to. The True/false method performs some calculations when implemented to determine whether the incoming data conforms to the criteria and returns a return of true, which is added to the List<t> collection that represents itself.
Now that we need to find all the even numbers from a list<int> collection, how do we do that?
1. Traditional methods
Public classmydelegatesyntax{ Public Static voidShow () {Console.WriteLine ("Fun with Lambdas"); List<int> list =Newlist<int> { -,1,4,8,9, the }; predicate<int> callback =Newpredicate<int>(Isevennumber); //Traditional Methodslist<int> evenlist =list. FindAll (callback); Console.WriteLine (); foreach(intIteminchevenlist) {Console.WriteLine (item); } } Private Static BOOLIsevennumber (intobj) = obj%2==0;}
2. Anonymous Methods
Public classmydelegatesyntax{
Public Static voidShow () {Console.WriteLine ("Fun with Lambdas"); List<int> list =Newlist<int> { -,1,4,8,9, the }; //Anonymous Methodslist<int> evenlist = list. FindAll (Delegate(inti) {returnI2==0; }); Console.WriteLine (); foreach(intIteminchevenlist) {Console.WriteLine (item); } }}
3.LAMBDA-expression
Public classmydelegatesyntax{ Public Static voidShow () {Console.WriteLine ("Fun with Lambdas"); List<int> list =Newlist<int> { -,1,4,8,9, the }; //lambda expression//implicitly, the editor will have a more contextual expression to infer the type of Ilist<int> evenlist = list. FindAll (i = i%2==0); //an explicit//Description: My parameter list (a reshape i) will be processed by the expression (i%2) ==0list<int> evenList1 = list. FindAll ((inti) = i%2==0); //my parameter list (a reshape i) will be processed by the expression (i%2) ==0Console.WriteLine (); foreach(intIteminchevenlist) {Console.WriteLine (item); } }}
4. Processing parameters with multiple statements ("{}")
Public classmydelegatesyntax{ Public Static voidShow () {Console.WriteLine ("Fun with Lambdas"); List<int> list =Newlist<int> { -,1,4,8,9, the }; //Multiple processing statement block {}list<int> evenlist = list. FindAll (i = { Console.WriteLine (i); return i% 2 = = 0 ; }); Console.WriteLine (); foreach(intIteminchevenlist) {Console.WriteLine (item); } }}
5. Lambda expression with multiple (or 0) parameters
Public classmydelegatesyntax{ Public Delegate stringverysimpledelegate (); Public Delegate voidMathmessage (stringMsgintresult); Public Static voidShow () {Console.WriteLine ("Fun with Lambdas"); //Lambda with multiple parametersmathmessage mm =NewMathmessage (msg, result) = Console.WriteLine ($"msg:{msg} Result:{result}")); MM ("Adding has cmpleted",1+5); //0 Parameter LambdaVerysimpledelegate d =NewVerysimpledelegate (() ="Enjoy you string"); Console.WriteLine (D.invoke ()); }}
Learning, hope you crossing a lot of advice.
C#lambda Expression Learning Diary