This article introduces the use of lambda expressions in C # as an example, and shares them for everyone's reference. Specific as follows:
From a delegate's point of view, there is no difference between a lambda expression and an anonymous method. In front
Article, we used an anonymous method to invoke the FindAll method of the list<t>. Starting with C # 3.0, in places where anonymous methods are used, you can completely replace them with lambda expressions. Lambda expressions are defined in the following way: "([parameter list]) = = expression". The operator "=" is a right-associative operator with the same precedence as the assignment operation "=", read in English: "Goes to".
Now look back at our example. The following code is associated with the previous
The code in the article has the same effect:
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);}}
The lambda Expression implementation above has no difference in effect from the anonymous method, and the name on the left of "= =" Defines the parameter (the parentheses can be omitted when the number of arguments is 1), and the execution body is defined on the right of "= =". Because the C # 3.0 compiler has the ability to type inference, parameter types and return values are determined by the compiler through the context, so unlike anonymous methods, the parameters of a lambda expression can be given without a parameter type. The lambda expression can also be used when the represented anonymous method does not have any parameters, just a pair of parentheses to the left of "= =". That
In fact, the term "lambda expression" is more general, in fact the "= =" operator can represent either a lambda expression or a lambda statement. Lambda statements are made up of blocks of code, much like anonymous methods. Take a look at the following example:
Class program{static void Main (string[] args) {//lambda expression func<int, bool> Dele1 = n ~ n >;//Lambda Statement Func<int, bool> dele2 = (int n) + = {return n > 10;}; Console.WriteLine (Dele1 (16)); Console.WriteLine (Dele1 (8)); }}
The two definition methods can also output the results correctly. Note that when we want to build an expression tree, things are completely different:
Okexpression<func<int, bool>> expr1 = n = n > 10;//error:cannot converted to an expression treeexp Ression<func<int, bool>> expr2 = (int n) + = {return n > 10;};
Thus, when building an expression tree, you cannot use lambda statements (lambda expressions with code statements), but you should use lambda expressions. From here you can see the difference between an anonymous method and a lambda expression.
It is believed that this article has some reference value to the study of C # program design.
In addition to the Declaration,
Running GuestArticles are original, reproduced please link to the form of the address of this article
Examples of lambda expression usages in C # Basics Tutorial
This address: http://www.paobuke.com/develop/c-develop/pbk23545.html
Related Content C # gets the first picture of the HTML text with the excerpt summary sample code in C # using the XmlDocument class to create and modify XML-formatted data files in C # Implement user-defined controls embedded in their own icons resolve private constructors and static constructors in C #
C # Implementation of the TreeView node drag and Drop method C # Implementation of the 12306 automatic login Method C # Push information to APNs Method C # Implements encryption and decryption of files
Examples of lambda expression usages in C # Basics Tutorial