C # Delegate--Basic 2

Source: Internet
Author: User

In the previous essay, a brief description of how to define a delegate, define an event, subscribe to an event, and finally achieve the effect is that when an object of an employee class executes an event, the delegate event is triggered and the result is obtained, but imagine the actual scenario, if an employee in the office plays a game, This time, we have to find out who, where to play the game, rather than to everyone to broadcast someone to play games, this does not make any sense. Therefore, it is necessary to improve the code in the previous article, so we must find a way to pass the parameters, the parameters need to contain the necessary information.

In this example, with regard to parameters, a parameter class was first created, Customeevebtargs and inherited from EventArgs (this is what I see as the "base class for the class that contains the event data")

1   Public classCustomeevebtargs:eventargs//parameter Class2     {3         stringName ="";//Employee Name4         intNumber =0;//Office Number5          PublicCustomeevebtargs ()6         {7         }8          Public stringName9         {Ten             Get{return  This. Name; One             Set{ This. Name =value;} A         } -          Public int Number -         { the             Get{return  This. number;} -             Set{ This. Number =value;} -         } -}

Use the object of this parameter class to save the relevant information. At this moment, the employee class should have the name and seat number, the code ibid.

1      //Define a delegate (a fox who hits a small report)2     /// </summary>3     /// <param name= "Sender" >the source of the triggering event</param>4     /// <param name= "E" >parameters carried by the trigger time</param>5      Public Delegate voidDelegateclasshandle (ObjectSender,customeevebtargs e);

At this point, the delegate event function should have the above two parameters when defining the delegate.

PlayGame (this, e);

This time this represents the instance, which is the instance that triggered the delegate event. E is the parameter information in this instance, where E is initialized as follows:

New Customeevebtargs ();            = _name;            = _number;

The initialized information is passed as a parameter as long as the employee object initialization is defined

 Public void Notify (object  Sender,customeevebtargs e)        {            Console.WriteLine (" report owner, {0}} in {1 } play the game Oh , E.name, ((Employee) sender). number);        }

Note: The function that handles the event must be preceded by the same as the delegate definition, as shown above. The above parameters can also be expressed in a variety of ways, either with the properties of the parameter class object or with the properties of the instance that triggered the event.

Of course, after the event is triggered, it can also be processed by multiple functions, such as adding a "penalty pay" function

static void penalty (object sender, Customeevebtargs e)
{
Console.WriteLine ("Fine 100");
}

So just my delegate function is associated with the function can be

E.playgame + = new Delegateclasshandle (penalty); I'm not recreating a class here, I'm just going to use the function as a static function, let the delegate call the function name directly, and don't forget to define the delegate with the same arguments when defining the function.

C # Delegate--Basic 2

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.