a sample of delegate & event


public delegate void NameListEventHandler(object sender, NameListEventArgs e);
public class NameListeventArgs:EventArgs
{
  string name;
  int count;
 
  public NameListArgs(string str, int i)
  {
    name = str;
    count = i;
  }

}

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 class EventDemo
{
   public static void main()
   {
     NameList names = new NameList();
     nameList.nameListEvent += new NameListEventHandler(NewName);

     names.Add("SMITH");
   }

   public static void NewName(object source, NameListEventArs args)
   {
     Console.WriteLine(args.Name+"was added to the list");
   }
}

阅读更多
个人分类: MS.Net
下一篇Dispose & Finalize
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭