Delegate and event in C # (3)-origin of the event

Source: Internet
Author: User

Event Origin

Let's continue to think about the above program: The above three methods are defined in the Programe class, so as to facilitate understanding. In actual application, GreetPeople is usually in a class, chineseGreeting and EnglishGreeting are in another class. Now you have a preliminary understanding of the delegation. It is time to improve the above example. Suppose we put GreetingPeople () in a class called GreetingManager, then the new program should look like this:

Namespace Delegate {
// Define the delegate, which defines the types of methods that can be represented
Public delegate void GreetingDelegate (string name );

// New GreetingManager class
Public class GreetingManager {
Public void GreetPeople (string name, GreetingDelegate MakeGreeting ){
MakeGreeting (name );
}
}

Class Program {
Private static void EnglishGreeting (string name ){
Console. WriteLine ("Morning," + name );
}

Private static void ChineseGreeting (string name ){
Console. WriteLine ("good morning," + name );
}

Static void Main (string [] args ){
//......
}
}
}

At this time, if you want to implement the output shown above, the Main method should be like this:

Static void Main (string [] args ){
GreetingManager gm = new GreetingManager ();
Gm. GreetPeople ("Jimmy Zhang", EnglishGreeting );
Gm. GreetPeople ("Zhang Ziyang", ChineseGreeting );
}

We run this code. Well, there is no problem. The program output as expected:
Morning, Jimmy Zhang
Good morning, Zhang Ziyang

Now, let's assume that we need to use the knowledge learned in the previous section to bind multiple methods to the same delegate variable. What should we do? Let's rewrite the code again:

Static void Main (string [] args ){
GreetingManager gm = new GreetingManager ();
GreetingDelegate delegate1;
Delegate1 = EnglishGreeting;
Delegate1 + = ChineseGreeting;

Gm. GreetPeople ("Jimmy Zhang", delegate1 );
}

Output:
Morning, Jimmy Zhang
Good morning, Jimmy Zhang

Here, we can't help but think of object-oriented design, which focuses on Object encapsulation. Since we can declare variables of the delegate type (in the above example, It is delegate1 ), why don't we encapsulate this variable in the GreetManager class? Isn't it more convenient to use in the client of this class? Therefore, we rewrite the GreetManager class as follows:

Public class GreetingManager {
// Declare the delegate1 variable in the GreetingManager class
Public GreetingDelegate delegate1;

Public void GreetPeople (string name, GreetingDelegate MakeGreeting ){
MakeGreeting (name );
}
}

Now, we can use this delegate variable as follows:

Static void Main (string [] args ){
GreetingManager gm = new GreetingManager ();
Gm. delegate1 = EnglishGreeting;
Gm. delegate1 + = ChineseGreeting;

Gm. GreetPeople ("Jimmy Zhang", gm. delegate1 );
}

Although this has achieved our desired results, it does not seem very elegant. Just register the first method with "=", and the second method with "+ =" makes people feel awkward. Now it's the turn of Event. in C #, events can be used to accomplish this task. We rewrite the GreetingManager class, which is like this:

Public class GreetingManager {
// This time we declare an event
Public event GreetingDelegate MakeGreet;

Public void GreetPeople (string name, GreetingDelegate MakeGreeting ){
MakeGreeting (name );
}
}

It is easy to note that the only difference between the MakeGreet event Declaration and the previous delegate variable delegate1 Declaration is that an event keyword is added. You can see that:The event is actually not hard to understand. Declaring an event is just like declaring a delegate-type variable..

We take it for granted to rewrite the Main method:

Static void Main (string [] args ){
GreetingManager gm = new GreetingManager ();
Gm. MakeGreet = EnglishGreeting; // compilation Error 1
Gm. MakeGreet + = ChineseGreeting;

Gm. GreetPeople ("Jimmy Zhang", gm. MakeGreet); // compilation Error 2
}

This time, you will get a compilation error: the event "Delegate. GreetingManager. MakeGreet" can only appear on the left side of the event + = or-= (except when it is used in the type "Delegate. GreetingManager ).

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.