摘要:Extension Method
01 using System;
02 using System.Linq;
03
04
05 namespace UsingWhereExtensionMethod
06 {
07 class Program
08 {
09 static void Main(string[] args)
10 {
11 var name = new[] { "Max", "Ginger", "HelloKitty" };
12
13 //使用 Where 函式,參數為 Delegate 實作
14
15 //標準的實作 Delegate
16 //var result = name.Where(myMethod);
17
18 //C#2.0的功能,此為匿名委派(Anonymous Delegate)
19 //var result = name.Where(
20 // delegate(string o)
21 // {
22 // return o == "Ginger";
23 // });
24
25 //在C#3.0新功能,Lambda Expression 使 Anoymous Delegate 寫法更簡單
26 var result = name.Where(a => a == "Ginger");
27
28 foreach(var v in result)
29 {
30 Console.WriteLine(v);
31 }
32 }
33
34 //實作 Delegate 必需符合 Where 函式中所定義的 Delegate signature
35 //(此範例為:回傳值 boolean,參數 string)
36 public static bool myMethod(string o)
37 {
38 return o == "Ginger";
39 }
40 }
41 }
02 using System.Linq;
03
04
05 namespace UsingWhereExtensionMethod
06 {
07 class Program
08 {
09 static void Main(string[] args)
10 {
11 var name = new[] { "Max", "Ginger", "HelloKitty" };
12
13 //使用 Where 函式,參數為 Delegate 實作
14
15 //標準的實作 Delegate
16 //var result = name.Where(myMethod);
17
18 //C#2.0的功能,此為匿名委派(Anonymous Delegate)
19 //var result = name.Where(
20 // delegate(string o)
21 // {
22 // return o == "Ginger";
23 // });
24
25 //在C#3.0新功能,Lambda Expression 使 Anoymous Delegate 寫法更簡單
26 var result = name.Where(a => a == "Ginger");
27
28 foreach(var v in result)
29 {
30 Console.WriteLine(v);
31 }
32 }
33
34 //實作 Delegate 必需符合 Where 函式中所定義的 Delegate signature
35 //(此範例為:回傳值 boolean,參數 string)
36 public static bool myMethod(string o)
37 {
38 return o == "Ginger";
39 }
40 }
41 }