C # series (2): In fact, delegation is very simple (below ),

Source: Internet
Author: User

C # series (2): In fact, delegation is very simple (below ),

In the previous blog, we learned that delegation is a data type used to declare a variable, but the variables declared by delegation can accept a method, as long as the method signature remains consistent.

 

We will continue to introduce delegation today.

(1) The essence of delegation is class:

Why is the essence of delegation a class? We know that when we use C # To compile the source code, we need to compile the project. The C # code we run is actually the compiled code. The compiled code becomes: microsoft intermediate code.

Then we create a new project, add only one class to the project, write our C # source code, and then compile the project. Let VS help us generate Microsoft intermediate code.

So let's take a look at how the compiled code of a class looks like. Create a new project and add a Program. cs class file to it, as shown below:

Well, write a Main method in Program as follows:

Then compile the project and let VS help us compile the C # Source Code as follows:

The code we compiled has been compiled successfully. We can find the compiled file in the folder where the project is located:

Then go to the debug Folder:

In, the first file is our compiled file: DemoForDelegate. The compiled file name is consistent with the name of the new project: DemoForDelegate.

Now we get the compiled file, which contains the intermediate code after compilation, so we need to check what the code in this file looks like now, you need a tool to view the information.

This tool is ILDasm, so we can find this tool in the System Disk of our computer. Because my system is installed on disk C, I can find it on disk C:

I found it in the directory C: \ Program Files (x86) \ Microsoft SDKs \ Windows \ v10.0A \ bin \ NETFX 4.6.1 Tools

Double-click it to open it, as shown below:

So, let's put the project we just compiled into it to see what the compiled intermediate code looks like:

After:

We can see that the compiled file contains the project: DemoForDelegate. The only Program class of this project is also included, so it is similar to what we think, and we should have it all.

At the same time, we can see below the Program class. class private auto ansi beforefieldinit ,. ctor: void (), Main: void (string []) members. They are the Program class, the constructor of the Program class, and the Main method. You only need to double-click the three members to view the compiled code. We can double-click. ctor: void () to see:

Now, we know how to view the C # compiled code and return to the delegate. Let's add a delegate in the Program class:

Compile again, and then check what happened in ILDasm:

From this we can see that the delegate is eventually compiled into a class with the same name, so the essence of the delegate is also a class.

 

(2) generic delegation:

Generally, delegates are used in our project, so we define several delegates:

1 namespace DemoForDelegate2 {3     public delegate void delegateInt(int m);4     public delegate void delegatedouble(double d);5     public delegate void delegatestring(string str);6     public delegate void delegatefloat(float f);7     public delegate void delegatedecimal(decimal d);8 }

Five delegates are defined to accept different methods.

However, if we look closely at the rule, we will find that these five delegates are very similar: the return types are all void, and the input parameters of the method are one. The difference is the data type of the parameter. So I want to simplify the code and write a common delegate to replace the preceding five delegates. Since their differences are the data types of parameters, so it is easy to think of using generics to make code generic, as shown below:

 1 namespace DemoForDelegate 2 { 3     //public delegate void delegateInt(int m); 4     //public delegate void delegatedouble(double d); 5     //public delegate void delegatestring(string str); 6     //public delegate void delegatefloat(float f); 7     //public delegate void delegatedecimal(decimal d); 8  9     public delegate void MyAction<T>(T arg);10 }

In this way, we can use a single delegate, so we can define more such generic delegation in our project:

1 namespace DemoForDelegate2 {3     public delegate void MyAction();4     public delegate void MyAction<T>(T arg);5     public delegate void MyAction<T1, T2>(T1 arg1, T2 arg2);6     public delegate void MyAction<T1, T2, T3>(T1 arg1, T2 arg2,T3 arg3);7     public delegate void MyAction<T1, T2, T3, T4>(T1 arg1, T2 arg2,T3 arg3,T4 arg4);8     //more...9 }

The preceding delegate does not return values. Let's define some delegate with return values:

1 namespace DemoForDelegate 2 {3 public delegate void MyAction (); 4 public delegate void MyAction <T> (T arg); 5 public delegate void MyAction <T1, T2> (T1 arg1, t2 arg2); 6 public delegate void MyAction <T1, T2, T3> (T1 arg1, T2 arg2, T3 arg3); 7 public delegate void MyAction <T1, T2, T3, t4> (T1 arg1, T2 arg2, T3 arg3, T4 arg4); 8 // more... 9 10 // delegate 11 public delegate TResult MyFunc <out TResult> (); 12 public delegate TResult MyFunc <out TResult, in T1> (T1 arg1 ); 13 public delegate TResult MyFunc <out TResult, in T1, in T2> (T1 arg1, T2 arg2); 14 public delegate TResult MyFunc <out TResult, in T1, in T2, in T3> (T1 arg1, T2 arg2, T3 arg3); 15 // more... 16 17}

For the delegate name above, take MyAction, MyFunc, haha. If you don't want to explain it, just a name, you know.

 

If you have more discussions about C # technology, you can add one of my QQ group: 240749438.

 

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.