public class Common 2 {
//【函数】查找数组中的奇数 4 public static List<int> FilterArrayOfInt(int[] ints, IntFilter filter) 5 { 6 var lstOddInt = new List<int>(); 7 foreach (var i in ints) 8 { 9 if (filter(i)) //用传进来的函数作判断 10 { 11 lstOddInt.Add(i); 12 } 13 } 14 return lstOddInt; 15 } 16 } 17
19 public class Application 20 {
//【函数】判断奇数偶数 21 public static bool IsOdd(int i) 22 { 23 return i % 2 != 0; 24 } 25 } 26
27
//声明数组
28 var nums = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
//调用 29 var oddNums = Common.FilterArrayOfInt(nums, Application.IsOdd); 30 foreach (var item in oddNums) 31 { 32 Console.WriteLine(item); // 1,3,5,7,9
33 }