zoukankan      html  css  js  c++  java
  • C#使用事件

    using System;
    namespace MyCollections
    {
       using System.Collections;

       public delegate void ChangedEventHandler(object sender, EventArgs e);
       public class ListWithChangedEvent: ArrayList
       {
           public event ChangedEventHandler MyAdd;

           public event ChangedEventHandler MyClear;
          protected virtual void OnAdd(EventArgs e)
          {
              if (MyAdd != null)
                  MyAdd(this, e);
          }
           protected virtual void OnClear(EventArgs e)
           {
               if (MyClear != null)
                   MyClear(this, e);
           }

          public override int Add(object value)
          {
             int i = base.Add(value);
             OnAdd(EventArgs.Empty);
             return i;
          }

          public override void Clear()
          {
             base.Clear();
             OnClear(EventArgs.Empty);
          }
       }

       class Test
       {

          // This will be called whenever the list changes.
          public static void AddChanged(object sender, EventArgs e)
          {
             Console.WriteLine("This is called when the event fires.    Add");
          }

           public static void ClearChanged(object sender, EventArgs e)
           {
               Console.WriteLine("This is called when the event fires. Clear");
           }
          public static void Main()
          {
           ListWithChangedEvent list = new ListWithChangedEvent();
           list.MyAdd += new ChangedEventHandler(AddChanged);
           list.MyAdd += new ChangedEventHandler(AddChanged);
           list.MyClear += new ChangedEventHandler(ClearChanged);
           list.Add("item 1");
           list.Clear();
           list.Add("item 1");
           list.Clear();
        
          }
       }
    }

  • 相关阅读:
    iOS- static extern const
    App 性能分析
    迭代器和生成器
    软件工程之个人软件开发----起步
    myeclipse调式与属性显示
    hdu 6188
    uva10780 质因子分解
    云服务器端口号的几个操作
    Redis错误 : MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk.
    ubuntu系统安装宝塔面板Linux版
  • 原文地址:https://www.cnblogs.com/zhangpengshou/p/1699895.html
Copyright © 2011-2022 走看看