偶是C#初学者,在书看到这段代码,用来做使用事件举例的,不过就是看不懂,这个事件是怎么触发的,还有代码中:
protected virtual void OnChaned (EventArgs e)
 {
  if(Added!=null)
  Added(this,e);
 }
一段为何用,麻烦解释一下意思!!!多谢多谢
using System;
using System.Collections;
public delegate void AddEventHandler(object sender, EventArgs e);
public class ListWithEvent:ArrayList
{
 public event AddEventHandler Added;
 protected virtual void OnChaned (EventArgs e)
 {
  if(Added!=null)
  Added(this,e);
 }
 public override int Add(object value)
 {
  int i=base.Add(value);
  OnChaned(EventArgs.Empty);
  return i;
 }
}
class EventMethod
{
 public void ListChanged(object sender,EventArgs e)
  {
   Console.WriteLine("This is called when the event fires.");
  }
}
class Program
{
 public static void Main()
{
 ListWithEvent List=new ListWithEvent();
 EventMethod test=new EventMethod();
 List.Added+=new AddEventHandler(test.ListChanged);
 List.Add("Item 1");
 List.Add("Item 2");
 Console.Read();
 }
}
麻烦高手们教教...

解决方案 »

  1.   


    protected virtual void OnChaned (EventArgs e)
    {
      if(Added!=null)
      Added(this,e);
    }你不是要问事件怎么触发的吗?这里就是啊。
    这个Added是一个事件,其它类或者对象可以绑定这个事情和它自己的某一个委托,这样当事件发生的时候这个委托就会被调用。而这个代码里的Added(this,e)正是当这个事件发生时,由这个事件源对象开始调用这一系列委托的代码。
      

  2.   

    这是当Added事件有要执行代码时,触发一个Added事件
      

  3.   

    http://a.alimama.cn/sinf.js