Instance:
Class Program
{
Static void Main (string [] args)
{
DelegateEvent de = new DelegateEvent ();
De. Do ();
Console. Read ();
}
}
Class DelegateEvent
{
Public void Do (){
FangDiChanPublisher fdc = new FangDiChanPublisher ();
BuyerSubscriber buyer = new BuyerSubscriber ();
Buyer. BuyerDemand = "80 m², 1 million RMB, three-bedroom, one-bedroom house ";
Fdc. PublisherHouseInfo ();
Fdc. FindHouseEvent + = new HouseDelegateHandler (buyer. BuyHouse); // buyer entrusts the intermediary to find the house
Fdc. FindHouse (); // events should be triggered through FindHouse ()
// Fdc. FindHouse ();
}
}
// Define delegate: Define a real estate agent. delegate is to pass the method (function) as a parameter
Public delegate void HouseDelegateHandler (string message );
// Define the event publisher
Public class FangDiChanPublisher
{
// Public HouseDelegateHandler FindHouseEvent;
Public event HouseDelegateHandler FindHouseEvent; // declare an event
Public static string BuyerDemand
{
Set;
Get;
}
Public void PublisherHouseInfo ()
{
Console. WriteLine ("Shenzhen XX Real Estate Agency published housing information: has a large number of advantageous housing, interested parties please contact: xxxxxxx ");
}
Public void FindHouse (){
// Do other things...
If (FindHouseEvent! = Null) // if a buyer entrusts an intermediary to look for a house
{
FindHouseEvent (BuyerDemand );
Find (); // Find a matched house in an existing house...
}
}
Public void Find ()
{
Console. WriteLine ("(Shenzhen XX Real Estate Agency) only took a short time to find the right house .");
}
}
// Define the event subscriber
Public class BuyerSubscriber
{
Public string BuyerDemand
{
Set {FangDiChanPublisher. BuyerDemand = value ;}
}
Public void BuyHouse (string demand)
{
Console. WriteLine ("buyer and real estate agent requests: {0}", demand );
}
}