zoukankan      html  css  js  c++  java
  • 【转载】#370

    You subscribe to a particular event in C# by defining an event handler-code that will be called whenever the event occurs (is raised). You then attach your event handler to an event on a specific object, using the += operator.

    Below is an example where we define an event handler for the Dog.Barked event. Each time that kirby barks, we'll record the date and time of the bark in a list.

     1 private static List<DateTime> barkLog = new List<DateTime>();
     2 
     3 static void Main()
     4 {
     5     Dog kirby = new Dog("Kirby", 12);
     6     kirby.Barked += new EventHandler(kirby_Barked);
     7 
     8     kirby.Bark();
     9     Console.ReadLine();
    10 
    11     kirby.Bark();
    12     Console.ReadLine();
    13 }
    14 
    15 // Neither argument is used, for the moment
    16 static void kirby_Barked(object sender, EventArgs e)
    17 {
    18     // Log kirby's barks
    19     barkLog.Add(DateTime.Now);
    20 }

    Assuming that the Dog class fires its Barked event whenever we call the Bark method, our handler will get called whenever kirby barks.

    原文地址:#370 - Subscribe to an Event by Adding an Event Handle

  • 相关阅读:
    AKKA学习(二) 未完
    AKKA学习(一)
    seata项目结构
    seata demo
    FESCAR
    GTS原理、架构
    Fescar使用(资料)
    高性能异步分布式事务TCC框架(资料汇总)
    TIDB学习资料
    自旋锁
  • 原文地址:https://www.cnblogs.com/yuthreestone/p/3595598.html
Copyright © 2011-2022 走看看