Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
/// delegate is an abstraction of the type of method
/ //The type of the method is defined by the return value, the number of parameters, and the type
// /So the delegate also defines different types of methods through the return value, the number of formal parameters, and the type
/// because the delegate is a type, which is a class, it needs to be instantiated when it is used.
The application of an instance object of a delegate type is flexible and is used in the same way as an instance object of a normal class
/// can be used as an empty shell for another method, or as a parameter of another method
The shell is called a method because the instance object does not have the real business logic capability,
/// is just one of the middle-handled people. If the teacher let Xiao Ming to fetch water, xiaoming is lame, walking inconvenience, but xiaoming
///// have a good friend Xiao Red, Xiao Ming let Xiao Red to fetch water, eventually the red hit back.
/// here, the equivalent of Xiaoming is entrusted.
namespace Delegate_test
{
Public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
}
<summary>
Defines the type of a method with an int return value
There are two parameters, both of which are int
</summary>
<param name= "x" ></param>
<param name= "Y" ></param>
<returns></returns>
public delegate int Culculatedelegate (int x, int y);
private void Form1_Load (object sender, EventArgs e)
{
Culculatedelegate cd = Added;
MessageBox.Show (Dele (CD, 3, 5). ToString ());
}
/// This method satisfies the type of method defined by the delegate
////he is a type of method that has a return value of int with two parameters, all of which are of type int
Span style= "FONT-SIZE:18PX;" >///here, the type of the method is determined by the return value, the number of parameters, and the type of the parameter.
//< Param name= "a" ></PARAM>
private int Added (int a, int b)
{
return a + B;
<summary>
The method has three parameters, the type is 1 delegate type (the type of the delegate method is 2 int parameters, an int return value), 2 int
</summary>
<param name= "CD" ></param>
<param name= "x" ></param>
<param name= "Y" ></param>
<returns></returns>
private int dele (culculatedelegate CD, int x, int y)
{
return CD (x, y);
}
}
}
Some understanding and imitation of delegates in C #