Step by Step C # Technical Discussion V. Events and delegation Mechanism

Source: Internet
Author: User

Overview

The delegate in C # is similar to the function pointer in C or C ++. Delegate allows programmers to encapsulate method references in the delegate object. The delegate object can then be passed to the code that can call the referenced method, without knowing which method will be called during compilation. Unlike function pointers in C or C ++, delegation is object-oriented and type-safe.

In C #, "Event" is a method in which a class can notify customers of an object when something happens. The most common purpose of an event is to use a graphical user interface. Generally, it indicates that the control class in the interface has some events. When you perform some operations on the control (such as clicking a button, these events will be notified.

Use delegation to declare events. The delegate object encapsulates a method so that the method can be called anonymously. An event is a method that allows the customer to delegate methods (the methods should be called when an event occurs) to it. When an event occurs, the delegate that the customer provides to it will be called.
Note: Delegation is the packaging of methods. It is used when you are not sure when you want to call a method, but cannot be implemented using abstraction or polymorphism.
 

Example of delegation in Observer mode:

Create PilotLamp. cs First:


Public interface PilotLamp
{
/// <Summary>
/// Green light
/// </Summary>
Void TurnOn ();

/// <Summary>
/// Notice
/// </Summary>
String Notice
{
Get;
Set;
}
}

Create DelegateEvent. cs again:


Public delegate void EventHandler (); then create TrafficLight. cs:


Public class TrafficLight: PilotLamp
{
Public event EventHandler Notices;
Private string notice;

# Region GreenLight Member

Public void TurnOn ()
{
If (Notices! = Null)
Notices ();
}

Public string Notice
{
Get
{
Return notice;
}
Set
{
Notice = value;
}
}

# Endregion
}

 

Create Driver. cs again:


Public class Driver
{
Private string Name;
Private PilotLamp greenLight;

Public Driver (string name, PilotLamp greenLight)
{
This. Name = name;
This. greenLight = greenLight;
}

Public void GoLeft ()
{
Console. WriteLine (string. Format ("{1} driver, {0}, please drive to the left.", greenLight. Notice, Name ));
}
}

Create Pedestrian. cs again:


Public class Pedestrian
{
Private string Name;
Private PilotLamp greenLight;
Public Pedestrian (string name, PilotLamp greenLight)
{
This. Name = name;
This. greenLight = greenLight;
}
Public void GoThrough ()
{
Console. WriteLine (string. Format ("{0} comrade, {1}, please move forward.", Name, greenLight. Notice ));
}
}

 

Finally, call:


Public partial class Run: Form
{
Public Run ()
{
InitializeComponent ();
}

Private void btnRun_Click (object sender, EventArgs e)
{
//-------------------------------------


TrafficLight trafficLight = new TrafficLight ();

Driver driverOne = new Driver ("James", trafficLight );
Driver driverTwo = new Driver ("Li Si", trafficLight );

Pedestrian pedestrianOne = new Pedestrian ("Wang Wu", trafficLight );
Pedestrian pedestrianTwo = new Pedestrian ("Ma ", trafficLight );

TrafficLight. Notices + = new Observer. EventHandler (driverOne. GoLeft );
TrafficLight. Notices + = new Observer. EventHandler (driverTwo. GoLeft );
TrafficLight. Notices + = new Observer. EventHandler (pedestrianOne. GoThrough );
TrafficLight. Notices + = new Observer. EventHandler (pedestrianTwo. GoThrough );
TrafficLight. Notice = "the green light is on .";
TrafficLight. TurnOn ();

//-------------------------------------

}

}

Select console application for output:

The result is as follows:

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.