委托是一种类型,委托的类是System.Delegate
String是种类型,String的类是System.String
定义一个String类型变量: public string str //string类型变量str
申明一个Delegate类型变量 public delegate int GetSumFun(int x,int y) //int GetSumFun(int x,int y)是一个整体,一个返回类型为int型,2个参数的方法,封装了一个返回类型为int型的方法GetSumFun
<pre class="csharp" name="code">namespace DelegateDemo{ public delegate double Calculation(int x, int y);//申明一个委托Calculation,封装了一个返回值为double,2个参数的方法Calculation,方法名Calculation就是委托的名称 //委托和类是同一单位对象,通过实例化调用 class Program { static void Main(string[] args) { double result = 0; Calculation delegateC = new Calculation(SumFun);//实例化。创建一个委托实例,把SumFun方法作为参数传给委托,封装方法 //重新给委托传递参数,方法名;重新封装方法 delegateC = new Calculation(AverageFun); result=delegateC(10, 20); Console.WriteLine(result); } static double SumFun(int num1, int num2) { return num1 + num2; } static double AverageFun(int num1, int num2) { return (num1 + num2) / 2; } }}using System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace Demo{ class DaYingJi { public void Stop() { Console.WriteLine("打印机关闭了!"); } } class HanJieJi { public void Stop() { Console.WriteLine("焊接机关闭了!"); } } class SaoMiaoYi { public void Stop() { Console.WriteLine("扫描仪关闭了!"); } } public class Controller { public delegate void StopControl(); public StopControl stopC; DaYingJi dyj = new DaYingJi(); HanJieJi hjj = new HanJieJi(); SaoMiaoYi smy = new SaoMiaoYi(); public Controller() { this.stopC += dyj.Stop; stopC += hjj.Stop; stopC += smy.Stop; } //static void Main(string[] args) { // Controller control = new Controller(); // control.stopC();//直接调用委托的所有方法,把委托的方法名作为方法名调用 //} }}
事件:< <pre class="csharp" name="code">using System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace Demo{ public class Test { public delegate string GetStrDelegate(string str1,string str2);//申明委托 public GetStrDelegate objDelegate; public Test() { objDelegate += fun1; objDelegate += fun2; } string fun1(string str1, string str2) { return str1 + str2; } string fun2(string str1, string str2) { return str1 + str2; } //static void Main(string[] args) { // string s = new Test().objDelegate("张", "三丰");//通过委托的对象调用 // Console.WriteLine(s); // string s2 = new Test().GetStrFun("张", "三丰"); // Console.WriteLine(s); //} public string GetStrFun(string str1, string str2) { return str1 + str2; } } public class EventDemo { public delegate string GetStrDelegate(string str1, string str2);//申明委托 public event GetStrDelegate objEvent;//申明事件,类型是上面定义的委托 事件就是委托的一个对象 //public GetStrDelegate objDelegate; //事件就多一个event关键字,其他都一样 public EventDemo() { objEvent += fun1; objEvent += fun2; } string fun1(string str1, string str2) { return str1 + str2; } string fun2(string str1, string str2) { return str1 + str2; } static void Main(string[] args) { string s= new EventDemo().objEvent("张", "三丰"); Console.WriteLine(s); } }}
</>< <pre class="csharp" name="code">事件的类型是委托
从button的Click事件分析委托和事件:</>< class="csharp" name="code"> public delegate void EventHandler(object sender, EventArgs e);//定义一个委托EventHandler.在system命名空间下。注意:System.EventHandler不是一个类,而是一个委托的定义,也就是委托的方法名</>< class="csharp" name="code"> </>< class="csharp" name="code"> </>< class="csharp" name="code">public event EventHandler Click //定义一个事件,类型是刚才定义的委托 EventHandler</>< class="csharp" name="code"> </>< class="csharp" name="code">this.button1.Click += new System.EventHandler(this.button1_Click);//用+=订阅事件.这里的button_click是一个方法名</>< class="csharp" name="code"> </>< class="csharp" name="code">//实际调用的方法,它的参数个数,参数类型要和委托的参数个数,和类型一致。
private void button1_Click(object sender, EventArgs e){
}
</>