1、Action 委托,没有返回值
class Program { public static void Main(string[] args) { Console.WriteLine("Hello World!"); List<int> mylist=new List<int>{0,2,3,4,5}; // var mylistEven=mylist.FindAll(p=>p%2==0); //多个语句处理参数 var mylistEven=mylist.FindAll(p=> { if(p%2==0) { return true; } else{ Console.Write(" 奇数"+p); return false; } }); Console.WriteLine("偶数显示 . . "); foreach(int i in mylistEven ) { Console.Write(" "+i); } Console.WriteLine(" "); // var args1=new MyArgs(); // var args2="myEvent"; // // var myEvenHandle=new EventHandler(this,(args1,args2)=>{ // // Console.WriteLine(" "); // }); // disable once SuggestUseVarKeywordEvident Action<int ,int > action1= delegate(int a,int b){ Console.WriteLine(a+" "+b); }; action1(1,2); Console.ReadKey(true); } } public class MyArgs:EventArgs { int age=0; }
2 、Func委托,可以包含返回值
class Program { static void Main(string[] args) { Func<int, bool> f1 = IsNumberLessThen5; bool flag = f1(4); Console.WriteLine(flag); //以下是其它的用法,与IsNumberLessThen5作用相同。只是写法不同而已。 Func<int, bool> f2 = i => i < 5; Func<int, bool> f3 = (i) => { return i < 5; }; Func<int, bool> f4 = delegate(int i) { return i < 5; }; flag = f2(4); Console.WriteLine(flag); flag = f3(4); Console.WriteLine(flag); flag = f4(4); Console.WriteLine(flag); Console.ReadLine(); } private static bool IsNumberLessThen5(int number) { if (number < 5) return true; return false; } }