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