委托,Lambda的几种用法

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

对此数组进行查询,查询小于5的数:

 方法1:( 使用查询表达式)

var lowNus = from n in numbers where n < 5 select n;


方法2 :var lowNus = numbers.Where(n => n < 5);
 
方法3:var lowNus = numbers.Where(delegate(int n) { return n < 5; });

方法4:

Func<int, bool> ourfunc = delegate(int n) { return n < 5; };

var lowNus = numbers.Where(ourfunc);

 
方法5:

Func<int, bool> myfunc = n => n < 5;

var lowNus = numbers.Where(myfunc );


方法6:

Expression<Func<int, bool>> exp = n => n < 5;

var lowNus = numbers.Where(exp.Compile());

 

以上方法的输出结果:

4,1,3,2,0

原文地址:https://www.cnblogs.com/ycdx2001/p/1422478.html