Some understanding and imitation of delegates in C #

Source: Internet
Author: User

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 #

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.