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

  • 相关阅读:
    高并发场景之RabbitMQ
    后台任务利器之Hangfire
    【Spring】Redis的两个典型应用场景--good
    【Spring】如何在单个Boot应用中配置多数据库?
    Spring Boot中集成Spring Security 专题
    RestTemplateIntegrationTests
    Spring REST实践之客户端和测试
    Android 高仿豌豆荚 一键安装app 功能
    使用Android studio下载github上的工程及问题解决
    Windows下AndroidStudio 中使用Git(AndroidStudio项目于GitHub关联)
  • 原文地址:https://www.cnblogs.com/zhangpengshou/p/1699895.html
Copyright © 2020-2023  润新知