C # Classic Programming--(Cat in life, mouse run, host is awakened) __ programming

Source: Internet
Author: User

One time I went to an interview, the interviewer had a life problem like this .

Please use the linkage effect to describe the problems in life, cat bark, the food is stealing the mouse scared away, and then the owner was awakened, please write the relevant code to solve such problems.

I see no thoughts, do not know how to do it, fortunately, in C # provides a call entrusted mechanism, can be a good solution to such problems.

delegates and events are widely used in the. Net framework, but better understanding of delegates and events is not easy for many people who have been in contact with C # for a long time. They are like a threshold son, past the threshold of the people, think it is too easy, and no past people see the delegates and events every time feel in the heart of panic, mixed body uncomfortable, this article will be the interview examples to explain the use of the delegation.

First we define a delegate, which is the syntax:

delegate void name ()

For example, the delegate void Mydelete ()-----indicates that the delegate should be bound to a method with a return value of void and without parameters

now we define a cat class:

public class Cat
{
<summary>
Declaring a delegate
</summary>
public delegate void Mydelete ();


}

The class declares a delegate, and we add this class to the following complete:

 public class Cat
    {
       ///<summary>
       ///declares a delegate
       ///</ Summary>
        public delegate void Mydelete ();
        ///<summary>
       ///the cat's name (what to do when the cat barks)
       ///</summary>
        public event Mydelete Catshut;

<summary>
A way to define a cat's bark
</summary>
public void Mycatshut ()
{
Console.WriteLine ("The Kitten Cries ...");
If this event is not empty (it means that the cat's call event is subscribed)
if (Catshut!= null)
{
Catshut ();
}
}
}

---We've written the cat's class.

Then we need the mouse and the host to subscribe to the cat call event, we need to define a mouse class and host class, as follows

public class Mouse
{
<summary>
The way the rat runs
</summary>
public void Run ()
{
Console.WriteLine ("The mouse heard the cat called, began to flee");
}
}

The only way a rat can run is because when the mouse hears the cat, it starts running.

Similarly, the master should have a way to wake up, as follows

public class people
{
<summary>
The way the host gets up
</summary>
public void WakeUp ()
{
Console.WriteLine ("The Master was awakened");
}
}

Now that the event source (cat) and the event Subscriber (mouse and owner) are already there, how do you subscribe to the cat call event?

We need to write one such method in the Test class, as follows:

Class Program
{
static void Main (string[] args)
{
Cat C = new Cat ();--Instantiation of a cat class
Mouse h = new Mouse ();---Instantiation of a mouse class
People p = new people ();---instantiate a master Class
C.catshut + = new Cat.mydelete (h.run);//the mouse registered an event called a cat
C.catshut+=new Cat.mydelete (p.wakeup); the owner also registered a cat called event
C.mycatshut (); --Cat bark (if not called this method, then the cat will not bark, the cat will not be called so the host and the mouse will not wake up and run)
Console.ReadLine ();
}


}

Operation effect will see a linkage effect, in fact, is also a design mode observer mode

At this time the cat is an event source (just like a mobile company launched a business), and both the host and the mouse are subscribers to the event (like a client to open the business) then, when the cat barks, the owner and the mouse receive the appropriate information (as long as the mobile company's subscription to this business, you can receive text messages, Customers who do not subscribe will not receive text messages. But when the mice and the cats were in harmony, the mouse is not afraid of the cat, it is not subscribed to the cat event, then the next time the cat is barking, the mouse will not run (when we canceled a mobile company's business so the next time it will not send a message to us is a truth)

This is a simple through the event delegation mechanism of a joint programming topic, for beginners, there is a certain bottleneck, strewn, I hope we can in the future study Yong climbing peak, this article aims to play a role in this.

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.