c#-Delegates and Events

Source: Internet
Author: User

To construct an event for a class, you must declare an delegate type field with event, such as:

Puclic CALSS test{
Public delegate EventHandler (object sender, EventArgs e); An event declared to be a delegate type;
}

Then you specify the name of an event and write out the processing statement:
Public event EventHandler Load

Define this "Load" event after creating an instance of the class:
Test m=new test ();
M.load=new EventHandler (m_load);
void M_load (object sender, EventArgs e)
{
MessageBox.Show ("This is a class event");
}

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); Associated 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 The color is {0}.") Please input: ", System.Console.BackgroundColor.ToString ());
}
}

public class EventClass
{
public delegate void Customeventhandler (object sender, EventArgs e);//First defines an object of a delegate type Customeventhandler

Declare an event with the delegate data type, with the event keyword, which defines two pieces of text;
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.ConsoleColor.Black;
Console.clear ();
}

}
}

In the class that contains the event declaration, declare a data type is a delegate of such an object Customeventhandler, it has two parameters (sender and E); The purpose of using a delegate here is to pass the method to the parameter in the run. And the instance generated by the delegate object receives the return value of this parameter method, therefore, when declaring an object of a delegate type, it should be defined according to the method structure in the class, or in a reference class the method structure that responds to the event should be generated based on the structure of the delegate object, such as what types of arguments they have, the type of the return value, That means the two should be consistent. Also, to properly use a delegate in C #, you must maintain three steps: declaration-instantiation-invocation.

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 an instance of the Customeventhandler object Customevent:public event Customeventhandler customevent;
3. A call to the event is implemented in the Invokeevent () method, referencing 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.