Delegate and delegate

Source: Internet
Author: User

Delegate and delegate

The c # delegate is to pass the function as a parameter.

In js, there is no need to delegate anything. You just need to convert it directly. No! Why!

 

A function is written in this way if its parameter is a function.

Public void method (Action <string, Int32> voidMethod, Func <string, Int32> returnMethod)

 

Action <string, Int32> voidMethod

It means that the function to be passed in is a function without return, that is, public void. It needs a str parameter.

(Here, every function in js is return, even if you haven't written it, it will return undefiend)

 

Func <string, Int32> returnMethod

This function is a function with return. The last Int32 parameter indicates that its return is an Int32, which is public Int32, all the parameters except the last one are required by this function.

 

So how to call it?

         method(delegate(string a, string b, Int32 c)            {                //do something             }, delegate(string a, string b)            {                //do something                 return 5;            });

 

The anonymous function delegate () {} is used here, because most js functions use anonymous functions.

 

Of course, if you have a hard time building an old school, you can write more traditional

Public void method (tuoguan voidMethod, tuoguan2 returnMethod) // You cannot write Action or Func here {Int32 I = returnMethod ("B"); voidMethod ("B ");} public delegate void tuoguan (string name); public delegate Int32 tuoguan2 (string name); public Int32 returnMethod (string name) {return 5;} tuoguan xx = delegate (string name) {// do somthing}; tuoguan2 yy = returnMethod; // you can point to a method, not necessarily delegate method (xx, yy );

 

Delegation is very useful. For example, the anonymous function that I use can also reference the variable in the scope. This is the same as js!

I think it is very good to use it as js!

 


Ask how commission and delegate distinguish between "delegation"

Commission
Part of speech and Explanation
N. Letter of appointment, appointment, Commission, Commission, offense, commission, and trust
Vt. Appointment, commissioned production, serving
[Commission] commissioned or commissioned by agents, brokers, and commissions

Delegate by delegate...

Commission is often used by companies in business.

Describes the delegate application in C #.

Introduction to the Delegate class
------------------------
Namespace: System
Assembly: mscorlib (in mscorlib. dll)

A Delegate class is a data structure that can reference static methods or reference class instances and class instance methods.
In previous interface programming, we should have been exposed to various types of event-driven processing modes,
In this mode, we define the function triggered by the corresponding event.
For example:
For the Click event of Button1, we can write functions such as Button1_Click or Btn1Clicked for driver processing.
The correspondence between events and driver functions is associated by the Delegate class.

In fact, the data structure of the Delegate class is somewhat similar to the function pointer in C/C ++.

A simple application of the Delegate class
------------------------
1. Define the data structure of a Delegate Function
2. Define the static method that Delegate will reference or reference the class instance and the class instance method
3. The Delegate data variable points to the instance method.
4. Use the Delegate data variable to execute the instance method

A very basic example (TestClass. cs ):

Using System;

Namespace MySample
{
Class TestClass
{
// 1. Define the data structure of a Delegate Function
Public delegate void GoDelegate ();

[STAThread]
Static void Main (string [] args)
{
// 3. The Delegate data variable points to the instance method
GoDelegate goDelegate = new GoDelegate (MyDelegateFunc );

// 4. Use the Delegate data variable to execute the instance method
GoDelegate ();
Return;
}

// 2. Define the static method that Delegate will reference or reference the class instance and the class instance method
Public static void MyDelegateFunc ()
{
Console. WriteLine ("delegate function ...");
}
}
}

Compilation execution result:

# TestClass.exe
Delegate function...

Use the Delegate class and Override to compare Polymorphism
-----------------------------------------------
1. When using the Delegate class, the following example can be clearly described.
1.1 first, define an animal base class (MyAnimalDelegateClass). The base class contains the public method of ShowAnimalType.
Call the instance method referenced by Delegate in the ShowAnimalType method.

1.2 defines two subclasses: LionDelegateClass and HorseDelegateClass. Del ...... remaining full text>

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.