C#高级语法基础知识总结5——委托&事件
委托
Delegate void IntMethodInvoker(int x);
Delegate double IntMethodInvoker(long x,long y);
委托的一种方式,把方法组合到一个数组中来使用,这样就可以在循环中调用不同的方法了。
核心语句ProcessAndDisplayNumber(operations[j], 2.0);
1 delegate double DoubleOp(double x); 2 3 4 DoubleOp[] operations = 5 6 { 7 8 MathOperations.MultiplyByTwo, 9 10 MathOperations.Square, 11 12 }; 13 14 for (int j = 0; j < operations.Length; j++) 15 16 { 17 18 Console.WriteLine("Using operations[{0}]:", j); 19 20 ProcessAndDisplayNumber(operations[j], 2.0); 21 22 ProcessAndDisplayNumber(operations[j], 7.94); 23 24 ProcessAndDisplayNumber(operations[j], 1.414); 25 26 Console.WriteLine(); 27 28 } 29 30 static void ProcessAndDisplayNumber(DoubleOp action, double value) 31 32 { 33 34 double result = action(value); 35 36 Console.WriteLine("Value is {0},result of operation is {1}", value, result); 37 38 } 39 40 class MathOperations 41 42 { 43 44 public static double MultiplyByTwo(double value) 45 46 { 47 48 return value * 2; 49 50 } 51 52 public static double Square(double value) 53 54 { 55 56 return value * value; 57 58 } 59 60 }
Action<T>和Func<T>委托
Action<in T>可调用一个参数的方法,Action<in T1,in T2>可调用带两个参数的方法
冒泡排序法实例,运用Func<T, T, bool>委托
核心语句:
static public void Sort<T>(IList<T> sortArray, Func<T, T, bool> comparison)
CreatDelegate.BubbleSorter.Sort(emplayees, CreatDelegate.Employee.CompareSalary);
代码:
1 CreatDelegate.Employee[] emplayees = 2 3 { 4 5 new CreatDelegate.Employee("苏国强",10000), 6 7 new CreatDelegate.Employee("盖茨",8000), 8 9 new CreatDelegate.Employee("乔布斯",9000), 10 11 new CreatDelegate.Employee("扎克伯格",7000), 12 13 }; 14 15 CreatDelegate.BubbleSorter.Sort(emplayees, CreatDelegate.Employee.CompareSalary); 16 17 foreach (var employee in emplayees) 18 19 { 20 21 Console.WriteLine(employee); 22 23 } 24 25 26 public class BubbleSorter 27 28 { 29 30 //冒泡排序 31 32 static public void Sort<T>(IList<T> sortArray, Func<T, T, bool> comparison) 33 34 { 35 36 bool swapped = true; 37 38 do 39 40 { 41 42 swapped = false; 43 44 for (int j = 0; j < sortArray.Count - 1; j++) 45 46 { 47 48 if (comparison(sortArray[j + 1], sortArray[j])) 49 50 { 51 52 T temp = sortArray[j]; 53 54 sortArray[j] = sortArray[j + 1]; 55 56 sortArray[j + 1] = temp; 57 58 swapped = true; 59 60 } } 61 62 } while (swapped); 63 64 } 65 66 } 67 68 69 70 public class Employee 71 72 { 73 74 public string Name { get; private set; } 75 76 public decimal Salary { get; private set; } 77 78 public Employee(string name, decimal salary) 79 80 { 81 82 this.Name = name; 83 84 this.Salary = salary; 85 86 } 87 88 89 90 public override string ToString() 91 92 { 93 94 return string.Format("{0},{1:C}", Name, Salary); 95 96 } 97 98 99 100 public static bool CompareSalary(Employee e1, Employee e2) 101 102 { 103 104 return e1.Salary < e2.Salary; 105 106 } 107 108 }
多播委托
Action<double> operations1 = MathOperations.MultiplyByTwo;//返回值void
operations1 += MathOperations.Square;
或
Action<double> operations1 = MathOperations.MultiplyByTwo; //返回值void
Action<double> operations2 = MathOperations.Square; //返回值void
Action<double> operations3 = operations1 + operations2; //返回值void
Delegate类定义GetInvocationList()方法,返回一个Delegate对象数组。
Action d1=One;
D1+=two;
Delegate[] delegates=d1.GetInvocationList();
匿名委托
匿名方法是用作委托的参数的一段代码
anonDel是这种委托类型的变量,不是把方法赋予这个变量,而是使用一段简单的代码:它前面是关键字delegate,后面是一个字符串参数。
string mid = "国"; Func<string, string> anonDel = delegate(string param) { param += mid; param += "强"; return param; }; Console.WriteLine(anonDel("苏"));
Lambda表达式
Console.WriteLine("Lambda表达式"); string mid1 = "国"; Func<string, string> anonDel1 = param=> { param += mid1; param += "强"; return param; }; Console.WriteLine(anonDel1("苏"));
多个参数的Lambda表达式
1 Func<double, double, double> twwoParams = (x, y) => x * y; 2 3 Console.WriteLine(twwoParams(3,4)); 4 5 //可为花括号中给变量添加参数类型 6 7 Func<double, double, double> towParamWithTypes = 8 9 (double x, double y) => x * y; 10 11 Console.WriteLine(towParamWithTypes(4, 2));
事件
1 2 #region 事件发布程序 3 4 public class CarInfoEventArgs : EventArgs 5 6 { 7 8 public string Car { get; private set; } 9 10 public CarInfoEventArgs(string car) 11 12 { 13 14 this.Car = car; 15 16 } 17 18 } 19 20 21 public class CarDealer 22 23 { 24 25 public event EventHandler<CarInfoEventArgs> NewCarInfo;//定义事件委托 26 27 28 29 public void NewCar(string car) 30 31 { 32 33 Console.WriteLine("CarDealer,新车{0}",car); 34 35 if(NewCarInfo!=null) 36 37 { 38 39 NewCarInfo(this,new CarInfoEventArgs(car)); 40 41 } 42 43 } 44 45 } 46 47 #endregion 48 49 #region 事件侦听器 50 51 public class Consumer 52 53 { 54 55 private string name; 56 57 public Consumer(string name) 58 59 { 60 61 this.name = name; 62 63 } 64 65 public void NewCarIsHere(object sender, CarInfoEventArgs e) 66 67 { 68 69 Console.WriteLine("{0}:车{1}是新的", name, e.Car); 70 71 } 72 73 } 74 75 #endregion 76 77 78 Console.WriteLine("事件-----------"); 79 80 var dealer = new DelegateEvent.CarDealer(); 81 82 var Soar = new DelegateEvent.Consumer("苏国强"); 83 84 dealer.NewCarInfo += Soar.NewCarIsHere; 85 86 dealer.NewCar("博兰基尼"); 87 88
弱事件管理器