例子如下:好好体会吧!using System; using System.Collections;namespace testevent { public class NameList { ArrayList list; public event NameListEventHandler nameListEvent; public NameList() { list=new ArrayList(); } public void Add(string Name) { list.Add(Name); if(nameListEvent!=null) { nameListEvent(this,new NameListEventArgs(Name,list.Count)); } } } public delegate void NameListEventHandler(object source,NameListEventArgs args); public class NameListEventArgs:EventArgs { string name; int count; public NameListEventArgs(string str,int i) { name=str; count=i; } public string Name { get { return name; } } public int Count { get { return count; } } } public class EventDemo { [STAThread] static void Main() { NameList names=new NameList(); names.nameListEvent+=new NameListEventHandler(NewName); names.nameListEvent+=new NameListEventHandler(CurrentCount); names.Add("flower"); names.Add("Bosworth"); Console.ReadLine (); } public static void NewName(object source,NameListEventArgs args) { Console.WriteLine(args.Name+" was added"); } public static void CurrentCount(object source,NameListEventArgs args) { Console.WriteLine("list current has "+args.Count+" item"); } }}
public class Demo { //定义一个代理(函数指针) private string myName; public string Name {set {this.myName = value;}} public delegate string myDelegate(string s); //定义一个事件(事件是靠代理来实现的) public event myDelegate myEvent; private void onChanged() { if (myEvent != null) myEvent(myName); }
public void myFunction() { //.... onChanged() //... } }public class ClientDemo { private Demo mydemo; private public ClientDemo(Demo d) {this.mydemo = d; //事件接受方帮定执行方法到该事件 myDemo.myEvent(this.myFunA(string s)) }
//这就是当事件fire时要执行的代码 private void myFunA(string yourName) {System.Console.WriteLing( "yourName is:" + yourName) ;} }public class myMain { public static void Main() { Demo d = new Demo; ClientDemo cd = new ClientDemo(d); d.Name= "BillGate"; d.myFunction(); // the "yourName is: BillGate" will be output } }-------------------- 没有调试,同时.net 规范中的event 要求有2个参数(事件发生方,事件数据(从System.EventArgs继承)) ,c# 的event 没有这样的要求
using System.Collections;namespace testevent
{
public class NameList
{
ArrayList list;
public event NameListEventHandler nameListEvent;
public NameList()
{
list=new ArrayList();
}
public void Add(string Name)
{
list.Add(Name);
if(nameListEvent!=null)
{
nameListEvent(this,new NameListEventArgs(Name,list.Count));
}
}
}
public delegate void NameListEventHandler(object source,NameListEventArgs args); public class NameListEventArgs:EventArgs
{
string name;
int count;
public NameListEventArgs(string str,int i)
{
name=str;
count=i;
}
public string Name
{
get
{
return name;
}
}
public int Count
{
get
{
return count;
}
}
}
public class EventDemo
{
[STAThread]
static void Main()
{
NameList names=new NameList();
names.nameListEvent+=new NameListEventHandler(NewName);
names.nameListEvent+=new NameListEventHandler(CurrentCount); names.Add("flower");
names.Add("Bosworth");
Console.ReadLine ();
}
public static void NewName(object source,NameListEventArgs args)
{
Console.WriteLine(args.Name+" was added");
}
public static void CurrentCount(object source,NameListEventArgs args)
{
Console.WriteLine("list current has "+args.Count+" item");
}
}}
{
//定义一个代理(函数指针)
private string myName;
public string Name
{set {this.myName = value;}}
public delegate string myDelegate(string s);
//定义一个事件(事件是靠代理来实现的)
public event myDelegate myEvent; private void onChanged()
{
if (myEvent != null)
myEvent(myName);
}
public void myFunction()
{
//....
onChanged()
//...
}
}public class ClientDemo
{
private Demo mydemo;
private
public ClientDemo(Demo d)
{this.mydemo = d;
//事件接受方帮定执行方法到该事件
myDemo.myEvent(this.myFunA(string s))
}
//这就是当事件fire时要执行的代码
private void myFunA(string yourName)
{System.Console.WriteLing( "yourName is:" + yourName) ;}
}public class myMain
{
public static void Main()
{
Demo d = new Demo;
ClientDemo cd = new ClientDemo(d);
d.Name= "BillGate";
d.myFunction();
// the "yourName is: BillGate" will be output
}
}--------------------
没有调试,同时.net 规范中的event 要求有2个参数(事件发生方,事件数据(从System.EventArgs继承)) ,c# 的event 没有这样的要求
这里有非常详细的说明,好好研究一下吧!,不太难