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 }