Interesting talk about delegation and events in c #

Source: Internet
Author: User

After the University expanded its enrollment, all types of universities or colleges emerged like Ox Hair. The phenomenon is the decline in the quality of university students, while the quality of university teachers is also less than the number of students, it is inevitable that a large number of non-standard teachers are mixed into all kinds of schools, so that the level of the class is reduced. As a result, more and more students are skipping classes...

As a member of many college students, the author is also in a second-stream school.

After the bell rings on a certain day, my dear brother gave me a lecture in the book "Management Information System". His eyes showed a few "good" students who were very sleepy, with a gloomy smile on his face, "Let's name it! Raise your hand! "At first glance, there was a mess under the stage, and the beauty squad leaders hurriedly picked up their cell phones and quickly followed the keyboard to edit text messages to urge class skipping students to attend classes.

As a member of the present, the author of this article witnessed the strange nature of today's college classrooms, and immediately appeared in his mind the prototype of this program.

First, we use three types of Teacher, Monitor, and Student to indicate teachers, class leaders, and class skipping students. The instructor has a naming method, and the class leader has a method of sending text messages to notify class skipping students to attend classes, class skipping has a way to send a text message to the class leader.

Next, we define a delegate and corresponding event in the Teacher Class, respectively:

public delegate void signHandler(bool param);
public event signHandler sign;

Add a private variable to the Teacher class to indicate whether the instructor has been named.

private bool isSigned;

The last step is to add a naming method for the Teacher Class. for the specific implementation of the method, see the code.

Public VoidSignName ()
{
Console. WriteLine ("I'm a teacher: Don't you come to class? Serious consequences!");
This. IsSigned= True;
//Name trigger
If(Sign! = Null)
{
Sign (isSigned );
}
}

So far, all the Teacher classes have been completed. The Monitor class representing the shift leader is very similar to the Teacher class. It has a sendHandler delegate, and the send event that encapsulates the delegate to send a notification message. Finally, the Monitor class also has a SendSMS method, which indicates the actions of the Monitor to send a notification text message. For specific implementation, see the code

ClassMonitor
{
Public Delegate VoidSendHandler (BoolParam );
Public EventSendHandler send;
//Private field indicating whether the monitor has sent a notification text message
Private BoolIsSent;
//The shift leader sent a text message to inform the students that they didn't come after seeing the instructor name.
Public VoidSendSMS (BoolParam)
{
If(Param)
{
Console. WriteLine ("I'm the shift leader: the instructor named me. Come and attend the class. I won't give it at the end of the semester!");
This. IsSent= True;
//Trigger a text message event
If(Send! = Null)
{
Send (isSent );
}
}
}
}

By now, most of the work has been completed. Finally, students who skip the course are displayed. This class is very simple. There is only one GoToClass method, indicating that the students will reply to the text message and rush to the classroom after receiving the notice from the shift leader.

ClassStudent
{
//Students will go to class after receiving a text message from the shift leader
Public VoidGoToClass (BoolParam)
{
If(Param)
{
Console. WriteLine ("I am a student: Report the shift leader. The text message has been received and is coming to class!");
}
}
}

Finally, let's look at how the main program is implemented. First, construct the example of the Teacher, shift leader, and student respectively, and then register the SendSMS method of the Monitor class to the sign event of the Teacher Class. This means that the leader's text message method has subscribed to the instructor's method, when the instructor calls the sign event, the shift leader immediately sends a text message to notify the students not to attend the class.

Then we need to register the GoToClass method of the class skipping student to the send event of the class leader. The class leader sends a text message indicating that the teacher has called the name of the student. The class skipping student has to rush to the class, and the credits are big.

class Program
{
static void

Related Article

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.