為拓展方法傳遞委托
阿新 • • 發佈:2018-11-10
sta orderby 結果 靜態方法 query oid write sele tel
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 string[] names = { "Burke", "Connor", "Frank", "Everett", "Albert", "George", "Harris", "David" }; 6 7 //Where擴展方法,要求的是委托實例 8 //01傳一個命名方法 9 var fiveNameA = names.Where(IsLengthFive);10 //02匿名方法 11 var fiveNameB = names.Where( 12 delegate(string s) { return s.Length == 5; } 13 ); 14 15 //03 靜態類中靜態方法的調用 16 IEnumerable<string> fiveNameC = Enumerable.Where(names, delegate(string s) { return s.Length == 5; }); 17 18 foreach (var item in fiveNameC) 19 { 20 Console.WriteLine(item.ToString()); 21 } 22 //04對03的結果排序 23 var fiveNameOrdered = fiveNameC.OrderBy( 24 delegate(string s) { return s; } 25 ); 26 foreach(var item in fiveNameOrdered) 27 { 28 Console.WriteLine(item.ToString()); 29 } 30 /**/ 31 Console.WriteLine("************************"); 32 //為擴展方法傳遞匿名方法(匿名委托實例) 33 IEnumerable<string> myQuery = 34 //names.Where(IsLengthFive) 35 names 36 .Where(delegate(string s) { return s.Length == 5; }) 37 .OrderBy(delegate(string s) { return s; }) 38 .Select(delegate(string s) { return "你好:" + s.ToLower(); }); 39 40 foreach (string item in myQuery) 41 { 42 Console.WriteLine(item); 43 } 44 45 46 47 48 } 49 50 private static bool IsLengthFive(string s) 51 { 52 //if (s.Length == 5) 53 //{ 54 // return true; 55 //} 56 //else 57 //{ 58 // return false; 59 //} 60 return s.Length == 5; 61 } 62 63 }
為拓展方法傳遞委托