Delegate and event - chat series (two)

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

  Series one tells some scene delegate and event in the life of this article, we C# language from the perspective on the principal meaning. We envision a following scene: the noon bell rang, you go to the school cafeteria food line, your girlfriend doesn't want to walk, let you pack a meal for her. The following code directly.:

public class You
{
   //Declare a delegate, is actually a &ldquo command;”
   public delegate void BringLunchEventHandler();

   //To approach the dining hall
    public static void HaveLunch()
    {
        Console.WriteLine("Go to the dining room to eat Oh!!!");
    }

    //Class girl
    class Girlfriend
    {
        public static void TakeLunch()
        {
           Console.WriteLine("But also help the girlfriend to bring food, really lazy.!");
        }

        public static void TaskCandy()
        {
           Console.WriteLine("To help girlfriend with sugar, alas, this woman, ah, no way!");
        }
    }


    public static void Main(string[] args)
    {
        //Here is specifically addressed this command is stem what of, in this case is you “ help girlfriend with rice”
        BringLunchEventHandler lunchDelegate = new BringLunchEventHandler(TakeLunch);
        lunchDelegate += Girlfriend.TakeCandy;//But also with sugar
        //This delegation is attached to the concrete method
        lunchDelegate();
        Console.ReadKey();
     }
}

  Girlfriend let you go with rice, she is only equivalent to the next instruction, you follow her to do it. And she told you what to buy, how much to buy, where to buy, this is equivalent to specifying a method signature, don't know so that we can better understand.

public delegate void BringLunchEventHandler();

  The above statement declaring that a principal, principal and method actually very similar: comparison method, commissioned a delegate keyword, does not have a method body <this is why trust is a command, it doesn't do anything>.

lunchDelegate += Girlfriend.TakeCandy;//But also with sugar

  Girlfriend let you go with rice, but also allows you to buy sugar, can also be other what. C#, through the + = operator can achieve delegation chain that multiple methods to bind to entrust,

When a delegate is invoked, the binding will be executed in sequence. Clever you should think of, - = operator is to cancel a method of binding.

   Additional: trust is a pointing method types, similar to the C++ function pointer, represents a function address. If int is a type, 2,3,4 are the values of this type; similarly, also commissioned a type, TakeLunch (), DoSomething (int count, String message) are the values of this type, so the delegate type represents a method.

   It is my understanding of the principal, if wrong place, welcome everybody! Next article will explain the events and client relationship in the same way!

 

      

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Kerwin at November 14, 2013 - 9:58 PM