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();
        
          }
       }
    }

  • 相关阅读:
    自动化测试常用断言的使用方法
    python接口自动化-有token的接口项目使用unittest框架设计
    postman
    HTML5基础
    HTML基础
    Web常见产品问题及预防
    JSON语法详解
    HTTP协议详解
    接口理论详解
    设计模式之装饰者模式
  • 原文地址:https://www.cnblogs.com/zhangpengshou/p/1699895.html
Copyright © 2011-2022 走看看