public delegate int Delegate(int i);//声明委托
public void F1(int i,int j);//no public int F2(int i);//no public int F3(int m,int n);//yes
namespace Test { delegate int Delegate(int i); class Program { public int F1(int i) { return i; } static void Main(string[] args) { Program p=new Program(); Delegate d1=new Delegate(p.F1);//创建实例 } } }
namespace Test { delegate int Delegate(int i); class Program<pre name="code" class="csharp">namespace Test { public delegate int Delegate(int i); public class Program { public static int F1(int i) { return i; } public int F2(int j) { return j; } static void Main(string[] args) { Program p=new Program(); Delegate d1=new Delegate(p.F1); Delegate d2=new Delegate(p.F2); Delegate d3=d1+d2;//给委托注册方法 int i3=d3(10); } } }
namespace Test { public delegate void Delegate(int i); public class Program { public static void F1(int i) { Console.WriteLine(i.ToString()); } public static void F2(int i) { Cosnole.WriteLine(i.ToString()); } static void Main(string[[] args) { Delegate d1=new Delegate(Program.F1); Delegate d2=new Delegate(Program.F2); Delegate d3=d1+d2; d1(10);//调用d1实例 d2(200);//调用d2实例 d3(201);//调用d3实例 } } }
public delegate void EventHandler(object sender,EventArgs e);
public event EventHandler Print;//申明事件Print
namespace Test { public delegate void EventHandler(object sender,EventArgs e) class Program { public event EventHandler Print; public void F1(object sender,EventArgs e) { Console.WriteLine("F1"); } public void F2(object sender,EventArgs e) { Console.WriteLine("F2"); } static void Main(string[] args) { Program p=new Program(); p.Print+=new EventHandler(p.F1);//给事件注册一个方法 p.Print+=new EventHandler(p.F2); if(p.Print!=null) { p.Print(null,null); } Console.Writeline(); p.Print-=new EventHandler(p.F1);//从事件中移除方法F1; if(p.Print!=null) { p.Print(null,null); } Console.ReadLine(); } } }
namespace Test { public delegate void EventHandler(object sender,EventArgs e); class Program { public event EventHandler Print; public void F1(object sender,eventargs e) { Console.WriteLine("F1"); } static void Main(string[] args) { Program p=new Program(); p.Print+=new EventHandler(p.F1); if(p.Print!=null) { p.Print(null,null);//调用事件 } Console.ReadLine(); } } }
原文地址:http://blog.csdn.net/u013470102/article/details/44850535