. Net event mechanism

Source: Internet
Author: User

I have been too busy over the past few days. I have reviewed my professional courses and have not read the books carefully for a few days. Well, let's talk about the events below. This is what you really understand, that is, a special case of delegation, it is a special delegate type (it can also be seen as a field but still returns the delegate type. Why, because even string STR is still a string type parameter ), as long as you understand this, it is not difficult to ask people to solve the problem and doubts. When I started reading it yesterday, I asked a few programmers who have developed it for several years. They also answered questions, in the end, I can't do it myself.

As I am about to go to class, I can only draw a mind chart and a demo at noon to show you my ideas. If you have different opinions, leave a message for discussion.

Example:

Using system;

Namespace event Research
{
// Parameter class containing the event
Class workeventargs: eventargs
{
Private string name;
Private string position;
Public string name
{
Get {return name ;}
}
Public String position
{
Get {return position ;}
}
Public String info
{
Get {return ("Staff Info ");}
}
Public workeventargs (string name, string position)
{
This. Name = Name;
This. Position = position;
}
}
// Define a delegate
Delegate void workeventhandler (Object sender, workeventargs E );
// Class that contains the event declaration and method for triggering the event
Class work
{
Private string name;
Private string position;
Public string name
{
Set {name = value ;}
Get {return name ;}
}
Public String position
{
Set {position = value ;}
Get {return position ;}
}
// Declare the event
Public event workeventhandler infodisplay;
// Method for triggering an event
Protected virtual void onwork (workeventargs E)
{
If (infodisplay! = NULL)
{
Infodisplay (this, e );
}
}
// Events
Public void diswork ()
{
Workeventargs E = new workeventargs ("", ""); // The parameter class is just convenient for us to use the delegate provided by. net.
Onwork (E );
If (name! = NULL & position! = NULL)
{
String S = string. Format ("Name: {0} \ n position: {1}", name, position );
Console. writeline (s );
}
// Initialize the parameter class of the event and extract information
Workeventargs c = new workeventargs (name, position );
Console. writeline ("Name: {0} \ n position: {1}", C. Name, C. Position );

}
}
// Class containing the Event Processing Method
Class researchdepartment
{
// Event handling method
Public void displayinfo (Object sender, workeventargs E)
{
Console. writeline (E. info );
(Work) sender). Name = "James ";
(Work) sender). Position = "department manager ";

}
Public void display (Object sender, workeventargs E)
{
Console. writeline ("the last two parameter classes initialize the event and extract information! \ N ");
}
Public void bookevent (Work worker)
{
Worker. infodisplay + = new workeventhandler (Display );
Worker. infodisplay + = new workeventhandler (displayinfo );
}

Void worker_infodisplay (Object sender, workeventargs E)
{
Throw new notimplementedexception ();
}
}
Class testevent
{
Public static void main ()
{
// Initialize the publisher
Work worker = new work ();
// Initialize the subscriber
Researchdepartment RD = new researchdepartment ();
// Subscribe to an event (that is, bind the event processing method to the delegate instance of the sender)
Rd. bookevent (worker );
Worker. diswork ();
Console. readkey (true );
}
}
}

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.