C #-delegate and event

Source: Internet
Author: User

To construct an event for the class, you must use the event to declare a delegate field, such:

Puclic calss Test {
Public delegate EventHandler (object sender, EventArgs e); // Events declared as delegate;
}

Specify the name of an event and write the processing statement:
Public event EventHandler Load

After creating an instance of the class, define the "Load" event:
Test m = new Test ();
M. load = new EventHandler (m_Load );
Void m_Load (object sender, EventArgs e)
{
MessageBox. Show ("this is a class event ");
}

Let's take a look at the complete code below:

Using System;
 
Class TestClass
{
Static void Main (string [] args)
{
EventClass myEventClass = new EventClass ();
MyEventClass. CustomEvent + = new EventClass. CustomEventHandler (CustomEvent1); // associate event handle;
MyEventClass. CustomEvent + = new EventClass. CustomEventHandler (CustomEvent2 );
MyEventClass. InvokeEvent ();
MyEventClass. CustomEvent-= new EventClass. CustomEventHandler (CustomEvent2 );
MyEventClass. InvokeEvent ();
MyEventClass. Load + = new EventClass. CustomEventHandler (Load1 );
MyEventClass. onLoad ();
}

Private static void CustomEvent1 (object sender, EventArgs e)
{
Console. WriteLine ("Fire Event 1 ");
}

Private static void CustomEvent2 (object sender, EventArgs e)
{
Console. WriteLine ("Fire Event 2 ");
}
Private static void Load1 (object sender, EventArgs e)
{
Console. WriteLine ("Current background color is {0}. Please input:", System. Console. BackgroundColor. ToString ());
}
}

Public class EventClass
{
Public delegate void CustomEventHandler (object sender, EventArgs e); // first define a delegate type object CustomEventHandler

// Declare an event with the delegate data type. Use the event keyword. Here two pieces are defined;
Public event CustomEventHandler CustomEvent;
Public event CustomEventHandler Load;

Public void InvokeEvent ()
{
CustomEvent (this, EventArgs. Empty );
}
Public void onLoad ()
{
Console. BackgroundColor = ConsoleColor. Red;
Load (this, EventArgs. Empty );
String s = Console. ReadLine ();
If (s! = "Yuping ")
{
Console. WriteLine ("You must type yuping for change it! ");
}
Else
{
Console. BackgroundColor = System. lelecolor. Black;
Console. Clear ();
}

}
}

In the class that contains the event declaration, the declaration of a data type is a delegated such object CustomEventHandler, which has two parameters (sender and e ); here, the purpose of delegation is to pass the method to the parameter during running, and the instance generated by the delegate object receives the return value of this parameter method. Therefore, when declaring a delegate object, you should define it according to the method structure in the class, or generate a method structure that responds to the event according to the structure of the delegate object in the reference class, for example, what types of parameters and return values are available for both of them, that is, they must be consistent. At the same time, to correctly use the delegate in C #, you must maintain three steps: declare-instantiate-call.

In the above Code, the EventClass class embodies this principle:
1. object declaring the delegate type: public delegate void CustomEventHandler (object sender, EventArgs e );
2. Create the meventhandler object instance CustomEvent: public event CustomEventHandler CustomEvent;
3. Call the event in the InvokeEvent () method and reference the event.

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.