C basic Lambda expression usage example tutorial

  • 2020-10-07 18:52:08
  • OfStack

This article introduces the use of Lambda expressions in C# in the form of examples and shares them with you for your reference. The details are as follows:

From a delegate perspective, Lambda expressions are no different from anonymous methods. In the previous C# base anonymous method 1, we used anonymous methods to call List < T > , FindAll method. Starting with C# 3.0, Lambda expressions can be used instead of anonymous methods. The Lambda expression is defined as: "([parameter list]) = > Expression ". "= operator > "Is a right associative operator of the same priority as the assignment" = ", pronounced in English as "goes to".

Now let's go back to our example. The following code has the same effect as the previous code in C# based anonymous method 1:


class Program 
{ 
 static void Main(string[] args) 
 { 
 List<string> names = new List<string>(); 
 names.Add("Sunny Chen"); 
 names.Add("Kitty Wang"); 
 names.Add("Sunny Crystal"); 
 
 List<string> found = names.FindAll 
  ( 
  // Lambda Expression Implementation 
  name => name.StartsWith( 
  "sunny", 
  StringComparison.OrdinalIgnoreCase) 
  ); 
 
 if (found != null) 
 { 
  foreach (string str in found) 
  Console.WriteLine(str); 
 } 
 } 
}

Lambda Expression Implementation above does not differ in effect from anonymous methods, "= > "name on the left defines the arguments (the parentheses can be omitted when the number of arguments is 1)," = > The right side defines the executor. Because the C# 3.0 compiler has the capabilities of Type Inference, parameter types and return values are determined by the compiler by context, so unlike anonymous methods, Lambda expressions can have parameters that are not given a parameter type. The Lambda expression can also be used when the represented anonymous method does not have any arguments, as long as the "= > The left hand side is indicated by 1 pair of parentheses. That is:


() => Console.WriteLine("Hello!"); 

In fact, the term "Lambda expression" is rather generic, in fact "= > The "operator can represent both Lambda expressions and Lambda statements. The Lambda statement consists of blocks of code that resemble anonymous methods in form. Here are some examples:


class Program 
{ 
 static void Main(string[] args) 
 { 
 // Lambda  expression  
 Func<int, bool> dele1 = n => n > 10; 
 // Lambda  statements  
 Func<int, bool> dele2 = (int n) => { return n > 10; }; 
 Console.WriteLine(dele1(16)); 
 Console.WriteLine(dele1(8)); 
 }
}

Both definition methods also output the results correctly. Note that when we want to build an expression tree, the situation is completely different:


// ok
Expression<Func<int, bool>> expr1 = n => n > 10;
// error: cannot converted to an expression tree
Expression<Func<int, bool>> expr2 = (int n) => { return n > 10; };

As you can see, instead of using Lambda statements (Lambda expressions with code statements), you should use Lambda expressions when building the expression tree. This is where anonymous methods differ from Lambda expressions.

It is believed that this article has a definite reference value for the learning of C# programming.


Related articles: