using System;
using System.Linq;
namespace myprog
{
class test
{
static void Main(string[] args)
{
int[] arr = new int[12] { 31,28,31,30,31,30,31,31,30,31,30,31};
int num = arr.Where(i => i % 2 == 0).Sum();
int num2 = (from i in arr where i % 2 == 0 select (int)i).Sum();
Console.WriteLine("偶数个数:"+num);
Console.WriteLine("偶数个数:"+num2);
Console.ReadKey();
}
}
}
运行一下;代码有2种写法,作用是一样,
结果不对;
arr.Where(i => i % 2 == 0).Sum(),这是linq写法,i写成其他字符都是一样;
错了,上面是求所有偶数的值的加总和;
把Sum改成Count就对了;