C # delegate and event)

Source: Internet
Author: User

Represents (Delegate):

It is a function pointer in the C # language, which indicates that it can point to a function and call the implementation of this function during running. Let's take a look at its implementation steps:

  1. Declare a delegate object.
  2. Implement functions with the same parameters and return values as delegate (which can be static and non-static ).
  3. When a delegate object is generated, pass the implemented function as a parameter to its constructor.

See the following example:

Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace UsingDelegate
{
Public delegate void MyDelegate (string mydelegate); // declare a delegate object

Public class TestClass
{

// Implement functions with the same parameters and return values
Public void HelloDelegate (string mydelegate)
{
Console. WriteLine (mydelegate );
}

// Implement static functions with the same parameters and returned values

Public static void HelloStaticDelegate (string mystaticdelegate)
{
Console. WriteLine (mystaticdelegate );
}
}

Class Program
{
Static void Main (string [] args)
{
TestClass testClass = new TestClass ();
MyDelegate mydelegate = new MyDelegate (testClass. HelloDelegate); // generates a delegate object
Mydelegate ("Hello delegate"); // call

MyDelegate myStaticDelegate = new MyDelegate (TestClass. HelloStaticDelegate); // generates a delegate object
MyStaticDelegate ("Hello static delegate"); // call
}
}
}

 

Event (Event):

Let me use an example to simulate the entire event process:

  1. Create a button class with a click event.
  2. Create a Form class with the button class defined above.
  3. Requirement: when a user clicks the button class, the From class processes the class and outputs a message "I know you have been clicked"

See:

 

First, we will click the button, and then the button will notify the Form, and then make the corresponding From. How should this process be implemented in C?

Below I will list the source code of the above example (here I will not introduce how to declare the event and so on ):

Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace UsingEvent
{
Public delegate void ClickEventHandler (object sender, EventArgs e); // declare a representative: See the Note at the end of the article.

Public class MyButton // create MyBottom
{
Public event ClickEventHandler ClickEvent; // declare an event

Public void Click () // Click MyButton
{
If (ClickEvent! = Null)
{
Console. WriteLine ("MyButton: I was clicked ");
ClickEvent (this, null); // throw an event to all corresponding users
}
}
}

Public class MyForm
{
Public MyButton myButton = new MyButton ();

Public MyForm ()
{

// Add the event to myButton. When myButton is clicked, the corresponding processing function is called.

MyButton. ClickEvent + = new ClickEventHandler (OnClickEvent );

}

// Event processing functions

Void OnClickEvent (object sender, EventArgs e)
{
Console. WriteLine ("MyForm: I know you have been clicked! ");
}
}

Class Program
{
Static void Main (string [] args)
{
MyForm form = new MyForm (); // generate a MyForm

Form. myButton. Click (); // Click the mouse in MyForm to display the effect.
}
}
}
 

Note: Public delegate void ClickEventHandler (object sender, EventArgs e); this is the standard declaration method for event delegation. In fact, we can not pass it in the parameter, or it can be of another type. But it is better to use the above declaration method. You can inherit EventArgs to wrap any other parameters you want to transmit.

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.