Action委托
1. 无参无返回值
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { //1. 使用new关键字和调用Invoke方法 Action action1 = new Action(ActionWithNoParaNoReturn); action1.Invoke(); //2. 直接将方法名复制给Action委托,直接调用(推荐) Action action2 = ActionWithNoParaNoReturn; action2(); //3. 匿名委托 Action action3 = delegate() { Console.WriteLine("无参无返回值"); }; action3(); //4. Lambda委托(推荐) Action action4 = () => { Console.WriteLine("无参无返回值"); }; action4(); Console.ReadLine(); } public static void ActionWithNoParaNoReturn() { Console.WriteLine("无参无返回值"); } } }
2. 单一参数无返回值
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { //1. 使用new关键字和调用Invoke方法 Action<String> action1 = new Action<String>(ActionWithPara); action1.Invoke("test"); //2. 直接将方法名复制给Action委托,直接调用(推荐) Action<String> action2 = ActionWithPara; action2("test"); //3. 匿名委托 Action<String> action3 = delegate(String value) { Console.WriteLine("这里是有参无返回值,参数值是:{0}", value); }; action3("test"); //4. Lambda委托(推荐) Action<String> action4 = (value) => { Console.WriteLine("这里是有参无返回值,参数值是:{0}", value); }; action4("test"); Console.ReadLine(); } public static void ActionWithPara(String value) { Console.WriteLine("这里是有参无返回值,参数值是:{0}", value); } } }
3. 多参数无返回值
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { //1. 使用new关键字和调用Invoke方法 Action<String, int> action1 = new Action<String, int>(ActionWithMulitPara); action1.Invoke("test", 1); //2. 直接将方法名复制给Action委托,直接调用(推荐) Action<String, int> action2 = ActionWithMulitPara; action2("test", 1); //3. 匿名委托 Action<String, int> action3 = delegate(String value1, int value2) { Console.WriteLine("这里有两个参数无返回值,参数值1是:{0},参数值2是:{1}", value1, value2); }; action3("test", 1); //4. Lambda委托(推荐) Action<String, int> action4 = (value1, value2) => { Console.WriteLine("这里有两个参数无返回值,参数值1是:{0},参数值2是:{1}", value1, value2); }; action4("test", 2); Console.ReadLine(); } public static void ActionWithMulitPara(String value1, int value2) { Console.WriteLine("这里有两个参数无返回值,参数值1是:{0},参数值2是:{1}", value1, value2); } } }
4. 将委托当做参数值传递
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { Test<string, int>(Action, "Hello World!", 2); Test<string, int>((p, q) => { Console.WriteLine("值1:{0}, 值2:{1}", p, q); }, "Hello World", 2);//使用Lambda表达式定义委托 Console.ReadKey(); } public static void Test<T1, T2>(Action<T1, T2> action, T1 t1, T2 t2) { action(t1, t2); } private static void Action(string s, int i) { Console.WriteLine("值1:{0}, 值2:{1}", s, i); } } }
5. 案例应用
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { public class Student { public String Name { get; set; } public int Id { get; set; } public int Age { get; set; } public int Sex { get; set; } } class Program { static void Main(string[] args) { List<Student> list = new List<Student>() { new Student(){Id=1,Name="张三",Age=19,Sex=1}, new Student(){Id=2,Name="李四",Age=20,Sex=2}, new Student(){Id=3,Name="王五",Age=23,Sex=1}, new Student(){Id=4,Name="赵六",Age=18,Sex=1} }; //1. ForEach方法遍历 Console.WriteLine("----ForEach方法遍历----"); list.ForEach(p => { Console.WriteLine("姓名:{0}, 年龄:{1}", p.Name, p.Age); }); //2. foreach循环遍历 Console.WriteLine("----foreach循环遍历----"); foreach (var item in list) { Console.WriteLine("姓名:{0}, 年龄:{1}", item.Name, item.Age); } Console.ReadLine(); } } }