1. Delegate is similar to a function pointer in C + +, defined as a class in C #
When declaring definitions, there are some like classes:
delegate int mydelegate (char c);
Instantiation: (Note that the function does not have parentheses, it is not a function call, but a function address)
MyDelegate Myfuncs = new MyDelegate (MYCLASS.TESTFUNC);
MyDelegate Myfuncs = Myclass.testfunc;
No parameter-free constructors are supported.
MyDelegate Myfuncs = new MyDelegate ();
To bind multiple functions of the same type to the same delegate, you can add a function multiple times:
Myfuncs + = Myclass.testfunc;
You can also remove a function
Myfuncs-= Myclass.testfunc;
The added functions are executed one by one in order of accession .
private void Button1_Click (object sender, EventArgs e)
{
MyDelegate mydelegate = new MyDelegate (changebyuttontext);
MyDelegate + = Changebyuttontext;
TTT T = new TTT ();
MyDelegate (t);
}
public delegate void MyDelegate (TTT t);
void Changebyuttontext (TTT t)
{
T.num + = 1;
Mybutton.text = "Changed" + (T.num);
}
public class TTT
{
public int num = 1;
}
The above program, each click event Trigger, T.num plus 2, of course, this is a reference to pass the variable, if the value is passed the variable to do the argument, it will only add 1
If it is only declared and not assigned such as MyDelegate MyFunc, then MyFunc cannot use the "+ =" symbol (like a null pointer)
The complete code is as follows:
Summarize
A delegate is a class that defines a method's type so that it can be passed as a parameter to another method, which dynamically assigns the method to the parameter, avoids the large use of the If-else (Switch) statement in the program, and makes the program more extensible.
2. Use of event and delegate
Take the button control in the form program as an example:
A) Create a new WinForm program that moves a button from the toolbox into the form, and a function in the Form1.cs program.
private void Button1_Click (object sender, EventArgs e)
{
}
In this function, you can declare the button's Click event. But how does the. NET call this function?
b) Check button1_click right-click "View All References", one of which is a reference from Form1.Designer.cs, click to find this code
This.button1.Click + = new System.EventHandler (This.button1_click);
(Note: This code is in the function private void InitializeComponent () in class Form1, and the public partial class Form1 is declared in Form1.cs: The form inherits from part of the Form1 class of the form Class (partial), and the definition of InitializeComponent () in its constructor is exactly in Form1.Desinger.cs)
The syntax of this code is much like delegate's "+ =" hook operation.
c) Check click, right-click "go To Definition" to go to the namespace System.Windows.Forms control class, where we find
public event EventHandler Click;
Click is an event, is a delegate (EventHandler),
D) Looking down in this class, there are a number of functions related to click events, one of which
protected virtual void OnClick (EventArgs e);
It is possible that the Click called in this step, that is, our own definition of the button1_click () function.
e) In order to verify, build a MyButton class, inherit the button. Overriding the OnClick function
public class Mybutton:button
{
public delegate void ClickEventHandler (object sender, EventArgs e);
public event ClickEventHandler Clickevent;
protected override void OnClick (EventArgs a) {
if (clickevent! = null)
{
This. Text = "MyButton: I clicked \ n";
Clickevent (this, null);
}
}
}
In the Form1 class, modify the following
Public Form1 ()
{
InitializeComponent ();
Mybutton.location = new Point (10,10);
Mybutton.size = new System.Drawing.Size (160,60);
Mybutton.text = "Hello";
This. Controls.Add (MyButton);
Mybutton.clickevent + = new Mybutton.clickeventhandler (onclickevent);
}
void Onclickevent (Object Sender,eventargs e)
{
Mybutton.text + = ("MyForm: I know you clicked");
}
The results of the operation are as follows:
3. The difference between event and delegate
A) delegate and classes are a level that can be written directly under the namespace without being sent into the class;
Event and function, property is a level that must be written in the class.
b) event to declare must first have a delegate (EventHandler)
delegate int mydelegate ();
Event MyDelegate Myevnthandler;
C # Note 06--delegate and Event