Lambda表达式的使用


在C#中,Lambda表达式运用的是Func等泛型

常用的有

Func<TResult> 委托

out TResult

此委托封装的方法的返回值类型。

Func 委托

in T

此委托封装的方法的参数类型。

out TResult

此委托封装的方法的返回值类型

 

 

应用

 

 

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

int[] ints2 = Array.FindAll(ints,x => x % 2 == 0); //取出偶数

foreach (int item in ints2)
{
   Console.WriteLine(item);
}

int c = ints.Count(x => x > 5);  //计算所有大于5的值的个数

Console.WriteLine(c);

int
s = ints.Sum(x => x > 5 ? x : 0); //计算所有大于5的值的和 Console.WriteLine(s); Console.ReadKey();