Use delegated intermediary rental to understand delegation and events

Source: Internet
Author: User

Delegation and events are widely used in. NET Framework. However, it is not easy for many people who have been in contact with C # for a long time to understand delegation and events. They are like a doorman. They think it is too easy to cross the past, and people without a past feel panic and uncomfortable every time they see a Commission incident.

I personally prefer to use object-oriented programming to understand logical programs and programming. The following describes how to use a delegated intermediary company to rent a house:

1. Define the entrusting and entrusting objects-the renters who want to rent a house but do not have a house, so they entrust an intermediary to look for a house;
2. Compile the method to delegate-rental agencies can find houses if they have houses;
3. The method to be delegated will be passed to the entrusted object-the renters come to the rental agency, and the entrusted renters will be sent to the intermediary company;
4. Use delegate to call the delegate method-an intermediary company starts to look for a house;

Using system; using system. collections. generic; using system. LINQ; using system. text; namespace use delegated simulated rental {class program {static void main (string [] ARGs) {customer = new customer ("Wang Tian", roomtype. onebedroom, 1000); agency = new agency (""); room room1 = new room (roomtype. onebedroom, 1000); room room2 = new room (roomtype. twobedroom, 3000); agency. roomlist. add (room1); agency. roomlist. add (R Oom2); customer. findroom = agency. findroom; customer. findroom () ;}/// <summary> // room type enumeration // </Summary> Public Enum roomtype {onebedroom, twobedroom, threebenroom} // <summary> // room class // </Summary> class room {public room (roomtype type, int price) {This. roomtype = type; this. price = price;} public roomtype {Get; set;} public int price {Get; Set ;}/// <summary> // customer class // /</Summary> Class Customer {public customer (string name, roomtype type, int price) {This. cusname = Name; this. wantprice = price;} Public String cusname {Get; set;} public roomtype wantroomtype {Get; set;} public int wantprice {Get; set;} Public Delegate void letagencytofindroom (roomtype, int roomprice); Public letagencytofindroom findroom; // method for the intermediary to find a house: Public void findroom () {Co Nsole. writeline (cusname + "said: I am looking for a" + wantroomtype + "house with a price not higher than" + wantprice); findroom (wantroomtype, wantprice );}} /// <summary> // intermediary class // </Summary> class agency {public agency (string name) {} Public String agencyname {Get; set ;} public list <room> roomlist = new list <room> (); Public void findroom (roomtype type, int roomprice) {console. writeline (agencyname + "the intermediary starts to look for a house"); room = NULL; Foreach (room R in roomlist) {If (R. roomtype = type & R. Price <= roomprice) {room = r ;}} if (room! = NULL) {console. writeline ("Find a set: Type:" + room. roomtype + "Price:" + roomprice);} else {console. writeline ("no suitable house found! ") ;}Console. Readline ();}}}

 

Use delegated intermediary rental to understand delegation and events

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.