Use of Delegate and event in C # (C # event-Support for publisher/subscriber Mode ))

Source: Internet
Author: User
Tags scream
From: http://www.cnblogs.com/gzhnan/articles/1896296.html

 

C # The event supports Publisher/subscriber mode. The Subscriber notifies the subscriber of the event, and the subscriber calls the registered event processing function when the event occurs.

First, you must define a type of Delegate and this type of delegate event in a class. Then, the event is triggered through a function in the class. When other external classes subscribe to this event, other classes will subscribe to this event to process it.Program. To complete information exchange. In this way, the biggest benefit is that the event trigger class does not care about all the processing programs of the event, but only about his own work and the events he cares about. Classes that are interested in this event will first subscribe to this event. When this event occurs, the event information will be captured and processed again.

For a simple example, there are two classes: Machine mechine class and Repair Engineer check class. If a fault occurs during the running of the mechine class, it will throw brokendown () information. If the check class subscribes to this event, it will capture the information and switch to the corresponding processing program.

Using system;

class mechine // defines a publisher class
{< br> Public Delegate void delegatename (string name ); // define a type of return command parameter proxy type
public event delegatename eventname; // define a delegate type event
Public void fun ()
{< br> If (eventname! = NULL) // if the event is not null, it means that a subscriber has registered the event, that is, the corresponding event processing function is called after the event is triggered.
{< br> console. writeline ("report, Machine Fault");
eventname (string _ name ); // trigger this event in the class
}< BR >}

Class check // a class that subscribes to the preceding events
{
Public void fix (string Str)
{
Console. writeline ("Please make" + STR + "Repair ");
}

}

Class Program
{
Static void main ()
{
Mechine mechine1 = new mechine (); // instantiate a mechine class
Check C1 = new check ();
Mechine1.eventname + = new mechine. delegatename (c1.fix );
// Use the fix function in the current class to subscribe to the eventname function in the mechine class. Note that the structure of the fix function must be the same as that of the event and proxy (the parameters and return values are all the same ), in this way, when an event in the mechine class is triggered, the system will notify the check class to go to the fix function.

Mechine. Fun ("James ");
}
}

The procedure is as follows:
1. Delegate definition delegate
2. Event definition event
3. event triggering
4. Event subscription and handling

Simple cat call, mouse run, host wake up

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace delegateevent
{
Public Delegate void cateventhandler (Object sender, eventargs E );
Public class cat
{
Public event cateventhandler catevent;
Public void scream (eventargs E)
{
If (catevent! = NULL) // is there a subscription?
{
Console. writeline ("cat name ...... ");
Catevent (this, e );
}
}
Public void mouse (Object sender, eventargs E)
{
Console. writeline ("rat run ...... ");
}
Public void people (Object sender, eventargs E)
{
Console. writeline ("host wake up ...... ");
}
Static void main ()
{
Cat cat = new CAT ();
Cat. catevent + = new cateventhandler (Cat. Mouse );
Cat. catevent + = new cateventhandler (Cat. People );
Cat. Scream (eventargs. Empty );
Console. Read ();
}
}

}

From: http://www.cnblogs.com/gzhnan/articles/1896296.html

 

C # The event supports Publisher/subscriber mode. The Subscriber notifies the subscriber of the event, and the subscriber calls the registered event processing function when the event occurs.

First, you must define a type of Delegate and this type of delegate event in a class. Then, the event is triggered through a function in the class. When other external classes subscribe to this event, other classes will subscribe to the handler for this event. To complete information exchange. In this way, the biggest benefit is that the event trigger class does not care about all the processing programs of the event, but only about his own work and the events he cares about. Classes that are interested in this event will first subscribe to this event. When this event occurs, the event information will be captured and processed again.

For a simple example, there are two classes: Machine mechine class and Repair Engineer check class. If a fault occurs during the running of the mechine class, it will throw brokendown () information. If the check class subscribes to this event, it will capture the information and switch to the corresponding processing program.

Using system;

class mechine // defines a publisher class
{< br> Public Delegate void delegatename (string name ); // define a type of return command parameter proxy type
public event delegatename eventname; // define a delegate type event
Public void fun ()
{< br> If (eventname! = NULL) // if the event is not null, it means that a subscriber has registered the event, that is, the corresponding event processing function is called after the event is triggered.
{< br> console. writeline ("report, Machine Fault");
eventname (string _ name ); // trigger this event in the class
}< BR >}

Class check // a class that subscribes to the preceding events
{
Public void fix (string Str)
{
Console. writeline ("Please make" + STR + "Repair ");
}

}

Class Program
{
Static void main ()
{
Mechine mechine1 = new mechine (); // instantiate a mechine class
Check C1 = new check ();
Mechine1.eventname + = new mechine. delegatename (c1.fix );
// Use the fix function in the current class to subscribe to the eventname function in the mechine class. Note that the structure of the fix function must be the same as that of the event and proxy (the parameters and return values are all the same ), in this way, when an event in the mechine class is triggered, the system will notify the check class to go to the fix function.

Mechine. Fun ("James ");
}
}

The procedure is as follows:
1. Delegate definition delegate
2. Event definition event
3. event triggering
4. Event subscription and handling

Simple cat call, mouse run, host wake up

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace delegateevent
{
Public Delegate void cateventhandler (Object sender, eventargs E );
Public class cat
{
Public event cateventhandler catevent;
Public void scream (eventargs E)
{
If (catevent! = NULL) // is there a subscription?
{
Console. writeline ("cat name ...... ");
Catevent (this, e );
}
}
Public void mouse (Object sender, eventargs E)
{
Console. writeline ("rat run ...... ");
}
Public void people (Object sender, eventargs E)
{
Console. writeline ("host wake up ...... ");
}
Static void main ()
{
Cat cat = new CAT ();
Cat. catevent + = new cateventhandler (Cat. Mouse );
Cat. catevent + = new cateventhandler (Cat. People );
Cat. Scream (eventargs. Empty );
Console. Read ();
}
}

}

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.