C# 3.0 unleashed (1)
lambdas is that they have minimal syntax and are transferable to and from expression trees. An expression
tree is the data representation of an expression, and the lambda is the executable representation of an expression.
lambada 表达式 语法
1.([parameter list]) =>
parameter list表示可选的参数列表, ‘=>’表示 goes to , become的含义。
2.使用lambada的好处.
One of the reasons you want to use lambdas is because of the shorter syntax,which is an improvement over anonymous method syntax.
3,delegate ,
- predicate<T>
- Action<T>
4. The .Net Framework Class Library (FCL) includes a set of Func<> delegates you can use in the majority of cases.
Here are the Func<> delegate overloads:
. Func<TResult>
. Func<T, TResult>
. Func<T1, T2, TResult>
. Func<T1, T2, T3, TResult>
. Func<T1, T2, T3, T4, TResult>
注意匿名方法和Lambada表达式的混合,以及使用.net原生代理以节省代码量和更好的表达语义。
实现这些func代理的好处就是可以reuse,如果使用anonymous method 就没法reuse。
if only have simple paramter ,the parentheses can’t require, but if the count of param more than one ,there must use paretheses ,
look like
Func<string, string, int> getMore = (a, b) => { return a.Length - b.Length; };
使用原生代理可以很好的实现模板方法。