Today, the expert discussed a problem:
Funclist.findall (pnodes = Pnodes.parentid = = "01")
Equivalent to the IF (Pnodes.parentid = = "") return pnodes;
is actually an abbreviation for a delegate function.
To summarize:
A LAMBDA expression is an anonymous function that can contain expressions and statements, and can be used to create a delegate or an expression tree type. All lambda expressions use the lambda operator =>, and the operator reads "goes to". The left side of the LAMBDA operator is the input parameter (if any), and the right side contains an expression or a block of statements. The LAMBDA expression x + x * x is read as "x goes to X times X".
Http://baike.baidu.com/view/3048187.htm
Windbey in order to enhance the access to the collection, MS designed list<t> such a generic collection, which has a lot of enhancements, such as Foreach,convertall,findall and so on, and in order to facilitate the use of MS in the system name space introduced a number of special delegate. Mainly include the following:
public delegate void action<t> (T obj); Used by ForEach
public delegate int comparison<t> (t x, t y); Used by Sort
Delegate TOutput Converter<tinput, toutput> (tinput input); Used by ConvertAll
delegate bool Predicate<t> (T obj); Used by FindAll
With these specially crafted delegate, coupled with the use of anonymous methods, we can get more concise and efficient code. Specific examples I have introduced before. Now in orcas, Ms adds the concept of lambda expressions. Lambda expressions are further enhancements to anonymous methods. It can be used to write new methods more conveniently. and semantically closer to humanity.
It also introduces a number of special delegate:
Public delegate T func<t> ();
Delegate T func<a0, t> (A0 arg0);
Public delegate T func<a0, A1, t> (A0 arg0, A1 arg1);
Delegate T func<a0, A1, A2, t> (A0 arg0, A1 arg1, A2 arg2);
Public delegate T func<a0, A1, A2, A3, t> (A0 arg0, A1 arg1, A2 arg2, A3 arg3);
In contrast to the special delegate in 2.0, you will find that they have many similarities:
public delegate int comparison<t> (t x, t y);
n Public delegate int func<t,t,int> (t arg0, t arg1);
Delegate TOutput Converter<tinput, toutput> (tinput input);
Delegate TOutput Func<tinput, toutput> (Tinput arg0);
Public delegate bool Predicate<t> (T obj);
Public delegate bool Func<t,bool> (T arg0);
That is, 3.0 of the special delegate is more generalized than 2.0, 2.0 is a special case of 3.0. So we can fully apply lambda expressions to some of the list<t> 's enhancement methods.
Sort method
List<int> list=new list<int> ();
The var numbers = new []{5, 4, 1, 3, 9, 8, 6, 7, 2, 0};
List. AddRange (numbers);
List. Sort (delegate (int a, int b)
24 {
return A.compareto (b);
26}
27);
//use Lambda
List. Sort ((a) =>a.compareto (b));
ConvertAll method
List<int> doublelist =list. Convertall<int> (delegate (int i)
21 {
I*2 return;
23});
//use Lambda
var doublelist2=list. Convertall<int> (i=>i*2);
FindAll method
List<int> lowerthanfivelist =list. FindAll (delegate (int i)
21 {
Return i<5;
23}
24);
var lowerthanfivelist2=list. FindAll (i=>i<5);
From the example above, we can see that the code written with lambda expressions is more concise and understandable. (after the code has been compiled test, not I invented.)
The above is the use of lambda expressions in 2.0. But when you're familiar with 3.0, you'll find that the enhancements provided by the 2.0 list<t> are completely superfluous. In fact, these enhancements are often not limited to list<t>, and are generally applicable to ienumerable<t> objects. However, if you change the Ienumable<t> interface then the impact is too large, will involve a lot of classes. So MS only provides these enhancements in list<t>. However, with a constructor of list<t>, you can make all Ienumerable<t> objects easily converted to List<t>, and then use these methods.
This can be said to be a very trickery method, but with the support of the 3.0 extension method, it is not so troublesome, and Ms also built a series of more powerful set operation methods.
For example, before the FindAll method, we can now write:
The var numbers = new []{5, 4, 1, 3, 9, 8, 6, 7, 2, 0};
The Var lowerthanfive=numbers. Where (i=>i<5); Never need list<t>, just operate on t[] or any other types implement ienumerable<t>
Max foreach (Var v in lowerthanfive)
Console.WriteLine (v);
ConvertAll method
var doublelist3=numbers. Select (i=>i*2);
Max foreach (Var v in DoubleList3)
Console.WriteLine (v);
Sort method
The Var orderlist =numbers. (I=>i);
Max foreach (Var v in orderlist)
Console.WriteLine (v);
There are even more powerful features:
For example, I want to take the maximum number of 5 in the numbers array.
The Var big5 =numbers. OrderByDescending (I=>i). Take (5);
Max foreach (Var v in Big5)
Console.WriteLine (v);
With Orcas's extension method and lambda expression, MS provides a more convenient and powerful function for collection operations. Standard Query Operators is not used here, otherwise the code will be simplified.
Of course, LINQ is now just a tech Preview version. There are still many deficiencies. Especially in IntelliSense:
1. var numbers = new []{5, 4, 1, 3, 9, 8, 6, 7, 2, 0}; When using this notation, numbers does not have IntelliSense capabilities.
2. When using a lambda expression, if you do not declare a type T, there is no IntelliSense for the variables in it. This is strange, and LINQ does not have a mandatory claim type.
var lowerthanfive=numbers. Where<int> (i=>i<5);
The Var lowerthanfive=numbers. Where (i=>i<5);
The above two kinds of writing should be no problem, obviously in the next way the variable I can not gain the ability of intelligent perception.
3. Numbers. By the way (...), when writing this method, numbers's IntelliSense does not have this method, but the compile run is not a problem.
4. Lambda expressions currently do not support multiple statements.
C # lambda expression