C # Tutorial C # events (event)

Source: Internet
Author: User

C # events (event)

Event is basically a user action, such as keystrokes, clicks, mouse movements, and so on, or something that appears, such as system-generated notifications. An application needs to respond to an event when an event occurs. For example, interrupts. Events are used for interprocess communication.

Using delegates through events

Events are declared and generated in a class, and are associated with an event handler by using a delegate in the same class or other class. The class that contains the event is used to publish the event. This is called the publisher (publisher) class. Other classes that accept the event are known as the Subscriber (Subscriber) class. Events use the Publish-subscribe (publisher-subscriber) model.

A publisher (publisher) is an object that contains event and delegate definitions. The connection between the event and the delegate is also defined in this object. This event is called by the publisher (Publisher) class object, and other objects are notified.

A Subscriber (subscriber) is an object that accepts an event and provides an event handler. The delegate in the publisher (publisher) class invokes a method (event handler) in the Subscriber (Subscriber) class.

declaring events (event)

When declaring an event inside a class, you must first declare the delegate type of the event. For example:

public delegate void Boilerloghandler (string status);

Then, declare the event itself, using the event keyword:

The event Boilerloghandler Boilereventlog is defined based on the delegate above;

The above code defines a delegate named Boilerloghandler and an event named Boilereventlog, which invokes the delegate when it is generated.

Example 1

Using System;namespace simpleevent{   using System;   public class Eventtest   {      private int value;      public delegate void Nummanipulationhandler ();      public event Nummanipulationhandler Changenum;      protected virtual void onnumchanged ()      {         if (changenum! = null)         {            changenum ();         }         else         {            Console.WriteLine ("Event fired!");         }      }      public eventtest (int n)      {         SetValue (n);      }      public void SetValue (int n)      {         if (value! = N)         {            value = N;            Onnumchanged ();   }}} public class MainClass   {public      static void Main ()      {         eventtest e = new Eventtest (5);         E.setvalue (7);         E.setvalue (one);         Console.readkey ();}}}   

When the above code is compiled and executed, it produces the following results:

Event fired! Event fired! Event fired!

Example 2

This example provides a simple application for troubleshooting hot water boiler systems. When the maintenance engineer checks the boiler, the temperature and pressure of the boiler are automatically recorded in the log file along with the maintenance engineer's notes.

Using system;using system.io;namespace boilereventappl{//Boiler classes class Boiler {private int temp;      private int pressure;         Public boiler (int t, int p) {temp = t;      pressure = p;      } public int gettemp () {return temp;      } public int getpressure () {return pressure;      }}//Event Publisher class Delegateboilerevent {public delegate void Boilerloghandler (string status);      The event Boilerloghandler Boilereventlog is defined based on the delegate above;         public void Logprocess () {String remarks = "O. K";         Boiler B = new boiler (100, 12);         int t = b.gettemp ();         int p = b.getpressure ();         if (T > | | | t < | | p < | | p > () {remarks = "need maintenance";         } onboilereventlog ("Logging info:\n");         Onboilereventlog ("temparature" + t + "\npressure:" + P);      Onboilereventlog ("\nmessage:" + remarks);   }   protected void Onboilereventlog (String message) {if (Boilereventlog! = null) {Boilere         Ventlog (message);      }}}//This class preserves the terms written to the log file class Boilerinfologger {FileStream fs;      StreamWriter SW;         Public Boilerinfologger (string filename) {fs = new FileStream (filename, filemode.append, FileAccess.Write);      SW = new StreamWriter (FS); } public void Logger (string info) {sw.      WriteLine (info); } public void Close () {SW.         Close (); Fs.      Close (); }}//Event Subscriber public class Recordboilerinfo {static void Logger (String info) {Console.WriteLine      (info); }//end of Logger static void Main (string[] args) {Boilerinfologger filelog = new Boilerinfologger ("e:\\         Boiler.txt ");         Delegateboilerevent boilerevent = new Delegateboilerevent (); Boilerevent.boilereventlog + = new Delegateboilerevent.boilerloghandLer (Logger); Boilerevent.boilereventlog + = new Delegateboilerevent.boilerloghandler (filelog.         Logger);         Boilerevent.logprocess ();         Console.ReadLine (); FileLog.      Close (); }//end of main}//end of Recordboilerinfo}

When the above code is compiled and executed, it produces the following results:

Logging info:temperature 100Pressure 12message:o. K

The above is the "C # Tutorial" C # Events (event) content, more relevant content please follow topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.