C # Delegate--Basic 1

Source: Internet
Author: User

In an object-oriented, high-level language, delegates have a wide range of uses, and of course, in C #, delegates are used in much the same way as they do with other programming languages, where the use of the delegate's syntax and usage logic are briefly talked about by self-learning about the contents of the delegate in C #.

Any technology is generated by demand, here is an example of a simple description of the parameters of the delegate requirement, and when implementing a delegate, the definition of the delegate, the definition of the event, the subscription of the event, the pass parameter, the multicast delegate. First of all, the production of the commissioned requirements (in many cases, the use of the Commission, the written definition is not too easy to understand, here is an example of a picture).

Can be simple to understand the delegation as shown, when the employee has done an event, this time, we define a delegate to monitor the event, immediately take action, you can imagine a delegate is a special conditional judgment, but here to judge the condition is not a simple if condition statement, but the occurrence of a specific event.

We simulate this scene in the office. First, create an employee employee class that defines a delegate public delegate void Delegateclasshandle ()[the definition of a delegate]outside the employee class; and create a delegate event in the Employee Class PlayGame (monitor the employee whether to playthe game) [the definition of the event], in the employee class there is also a play game this event game (), since it is to monitor whether the employee is playing games, it can be played games game () Add event PlayGame to this method () this can be imagined as a monitor, once the employee played the game will be captured by him, in the definition of the delegate event PlayGame is no parentheses, and in use, do not forget the parentheses, this is the syntax requirements, There are, of course, a lot of simplified grammar, but as beginners master the most common kind of method. The specific code is as follows:

1 public class   {3 public event        Delegateclasshandle playgame;//declaring the event of the delegate type Note keyword ' event ' You can now treat the defined delegate name Delegateclasshandle as a type 4  5 public        void       {7            if (playgame!=null) 8            Console.WriteLine ("I'm playing a game" }11}   

At this point we define the employee class. Next, as shown, we can create a reporter class with a small report, in fact it is quite simple to do, as long as the employee is found playing the game (that is, triggering the event), then execute the corresponding code. To snitch this type of code is as follows:

    {3 public         void        {5             Console.WriteLine ("Report boss, someone is playing a game oh"        }7     } 

At this time, we have commissioned, there are commission events, there are events that trigger the delegate event, but how do we need to associate it? This will require a [subscription event]. Specifically, we can do this in the "Office" that is the main () function.

1 static void Main (string        {3             employee E = new Employee (); 4             Repoter re = new repoter (); 5
       
        //indicates which method of the game is to be processed by the 6             
          console.readkey ()  After the event is triggered. 9}
         

When once executed to E. Game () This line of code, when executed to the PlayGame event, triggers the event being delegated and indicates which method is being processed. The results of the operation are as follows:

C # Delegate--Basic 1

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.