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

  • 相关阅读:
    【未完待续】MVC 之HTML辅助方法
    【部分】ASP.NET MVC5
    【总结】Github通过Git Bash上传文件到仓库
    【总结】委托和匿名委托的比较
    函数进化到Lambda表达式的三过程
    C# 常用linq、lambda表达式整理 【转】
    Lambda表达式用在什么地方?如何使用?
    【错误】fatal: destination path already exists and is not an empty directory. 错误及解决办法
    GIT更换连接方式
    Github中添加SSH key
  • 原文地址:https://www.cnblogs.com/zhangpengshou/p/1699895.html
Copyright © 2011-2022 走看看