public static void Print(IEnumerable myList) { int i = 0; foreach (Object obj in myList) { if (obj is Student)//这个是类型的判断,这里Student是一个类或结构 { Student s=(Student)obj; Console.WriteLine("\t[{0}]:\t{1}", i++, s.Sname); } if (obj is int) { Console.WriteLine("INT:{0}",obj); } } Console.WriteLine(); }
List<string> fruits = new List<string> { "apple", "orange", "banana" }; //去遍历 IEnumerable<string> query = fruits.Where(fruit => fruit.Length == 6); foreach (var q in query) { Console.WriteLine(q); } Console.ReadLine();
private void button1_Click(object sender, EventArgs e) { double a= 11.0; double b = 2.0; double c = 0.1; double d = 6.0; bool flag = Waring(a,b,c,d); } static bool Waring(params double[] numbers) { if (numbers == null || numbers.Length == 0) { throw new ArgumentException(); } return numbers.Any(i => i < 0 || i > 10); }
注意:IEnumerable的any和all 方法 。编程
any表示肯定序列中的任何元素是否都知足条件,all表示肯定序列中的全部元素是否都知足条件数组
举个栗子:app
List<string> fruits = new List<string>() { "apple", "banana", "orange" }; bool flag = fruits.All(i => { Console.WriteLine(i); return (i.Length > 3); });
all时 将输出结果:apple banana orangeui
any时将输出结果:applespa
所以 any当序列中有元素知足条件后,就不接下去判断了 直接出truecode
Any是只要有一个知足结果就是true,不然是false。
All是只要有一个不知足结果就是false,不然是true。blog