C # Lambda expression learning diary,
Lambda expressions only use a simpler way to write anonymous methods, which completely simplifies the use of. NET Delegate types.
Now, if we want to use the FindAll () method of the generic List <>, this method can be used when you extract a subset from a set.
// The unique parameter of this method is a System. predicate <T> type generic delegate public List <T> FindAll (Predicate <T> match ); // This delegate directs to any type parameter as the unique input parameter and returns the bool public delegate bool Predicate <in T> (T obj );
When FindAll () is called, each item in List <T> will pass in the method pointed to by the Predicate <T> object. When the method is implemented, some calculations are executed to determine whether the input data meets the standard, and true/false is returned. If true is returned, this item is added to the List <T> set that represents the current user.
Now, how can we find all the even numbers from a List <int> set?
1. Traditional Methods
Public class MyDelegateSyntax {public static void Show () {Console. writeLine ("fun with lambdas"); List <int> list = new List <int> {20, 1, 4, 8, 9, 77 }; predicate <int> callback = new Predicate <int> (IsEvenNumber); // List using the traditional method <int> evenList = list. findAll (callback); Console. writeLine (); foreach (int item in evenList) {Console. writeLine (item) ;}} private static bool IsEvenNumber (int obj) => obj % 2 = 0 ;}
2. Anonymous Method
Public class MyDelegateSyntax {
Public static void Show () {Console. writeLine ("fun with lambdas"); List <int> list = new List <int> {20, 1, 4, 8, 9, 77 }; // anonymous method List <int> evenList = list. findAll (delegate (int I) {return I % 2 = 0 ;}); Console. writeLine (); foreach (int item in evenList) {Console. writeLine (item );}}}
3. Lambda expressions
Public class MyDelegateSyntax {public static void Show () {Console. writeLine ("fun with lambdas"); List <int> list = new List <int> {20, 1, 4, 8, 9, 77 }; // Lambda expression // implicit. The editor will use a more contextual expression to deduce the List of I types <int> evenList = list. findAll (I => I % 2 = 0); // explicit // Description: My parameter list (an integer I) will be expressed by the expression (I % 2) = 0 processing List <int> evenList1 = list. findAll (int I) => I % 2 = 0); // my parameter list (an integer I) will be expressed (I % 2) = 0 processing Console. writeLine (); foreach (int item in evenList) {Console. writeLine (item );}}}
4. Process Parameters Using multiple statements ("{}")
Public class MyDelegateSyntax {public static void Show () {Console. writeLine ("fun with lambdas"); List <int> list = new List <int> {20, 1, 4, 8, 9, 77 }; // multiple processing statement blocks {} List <int> evenList = list. findAll (I =>{Console. WriteLine (I); return I % 2 = 0;});Console. WriteLine (); foreach (int item in evenList) {Console. WriteLine (item );}}}
5. Lambda expressions containing multiple (or zero) Parameters
Public class MyDelegateSyntax {public delegate string VerySimpleDelegate (); public delegate void MathMessage (string msg, int result); public static void Show () {Console. writeLine ("fun with lambdas"); // Lambda MathMessage mm = new MathMessage (msg, result) => Console. writeLine ($ "msg: {msg} result: {result}"); mm ("adding has cmpleted", 1 + 5 ); // 0 Lambda VerySimpleDelegate d = new VerySimpleDelegate () => "enjoy you string"); Console. writeLine (d. invoke ());}}
There is no end to learning. I hope you can give me more advice.