C # Delegate and event in basic series-C # (3)

Source: Internet
Author: User

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 );
}
}

 

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.