Events are the most commonly used elements in .Net programming. Whether in WINFrom development, form loading (Load), drawing (Paint), initializing (Init), etc.
I believe no one is unfamiliar with the code "protected void Page_Load(object sender, EventArgs e)". If you are careful, you will definitely find that many event methods have the two parameters "object sender, EventArgs e". Is this very similar to the delegation?
1. Authorization (also called delegation in some books)
What is a commission? The meaning of this name has given us the space we imagined, you are programming, you are writing a web page now, and JS is something you are not familiar with, so you commissioned a colleague of yours to help you complete the JS part. This is the commission, giving others what you can't do. And how do you know who did it? Of course you have to know the name! In order to distinguish different people with the same name, it is necessary to describe a feature.
In C#, the role of a delegate is described as follows: a delegate is like a pointer to a function, which can be used to call different functions when the program is running. This is actually the same as when you entrust a colleague to complete the JS code. If two colleagues can do this, they only need to achieve your needs (like an interface). Although the process they do is different and the results they make are also different, they can meet your requirements.
1. Simple commission
So what information does the entrustment need to bear? First, it stores the method name, the parameter list (method signature), and the returned type. for example:
delegate string/*Return type*/ ProcessDelegate(int i);
This is the definition of a delegation. The blue part is the keyword that declares the delegation, the red part is the type returned, and the black part is the type name of the delegation, which is similar to a class name, and the parameter part in () is the parameter part. It means that if you want to use this delegation to do things, then the method of doing things must meet the following conditions:
1. The return type and the return type of the delegate are the same, here is the string type;
2. Can and can only have one parameter, and it is of type int.
OK, if the above two conditions are met, everything will work :)
For example:
using System; using ; using ; namespace TestApp { /// <summary> /// Entrust /// </summary> /// <param name="s1"></param> /// <param name="s2"></param> /// <returns></returns> public delegate string ProcessDelegate(string s1, string s2); class Program { static void Main(string[] args) { /* Call method */ ProcessDelegate pd = new ProcessDelegate(new Test().Process); (pd("Text1", "Text2")); } } public class Test { /// <summary> /// method /// </summary> /// <param name="s1"></param> /// <param name="s2"></param> /// <returns></returns> public string Process(string s1,string s2) { return s1 + s2; } } }
The output result is:
Text1Tex2
2. Generic Delegation
Generic delegates are the type of parameters that are uncertain, for example, the code is rewritten as:
using System; using ; using ; namespace TestApp { /// <summary> /// Entrust /// </summary> /// <param name="s1"></param> /// <param name="s2"></param> /// <returns></returns> public delegate string ProcessDelegate<T,S>(T s1, S s2); class Program { static void Main(string[] args) { /* Call method */ ProcessDelegate<string,int> pd = new ProcessDelegate<string,int>(new Test().Process); (pd("Text1", 100)); } } public class Test { /// <summary> /// method /// </summary> /// <param name="s1"></param> /// <param name="s2"></param> /// <returns></returns> public string Process(string s1,int s2) { return s1 + s2; } } }
The output result is:
Text1100
The detailed content of generics does not fall within the scope of this article, so I won't say more here.
2. Events
When something happens, one object can notify another through an event. For example, when the front desk has completed the front desk interface, it notifies you that you can integrate the front desk with the programs you developed. This is an event. It can be seen that the event triggers another thing at one time node, and he will not care about how to do another thing. As for the incident, the key point is when and who will do it.
In C#, the time definition keyword is event. For example:
event ProcessDelegate ProcessEvent;
The entire event definition method and execution process:
using System; using ; using ; namespace TestApp { /// <summary> /// Entrust /// </summary> /// <param name="s1"></param> /// <param name="s2"></param> /// <returns></returns> public delegate void ProcessDelegate(object sender, EventArgs e); class Program { static void Main(string[] args) { /* The first step is executed */ Test t = new Test(); /* The associated event method is equivalent to finding the principal */ += new ProcessDelegate(t_ProcessEvent); /* Enter Process method */ (()); (); } static void t_ProcessEvent(object sender, EventArgs e) { Test t = (Test)sender; t.Text1 = "Hello"; t.Text2 = "World"; } } public class Test { private string s1; public string Text1 { get { return s1; } set { s1 = value; } } private string s2; public string Text2 { get { return s2; } set { s2 = value; } } public event ProcessDelegate ProcessEvent; void ProcessAction(object sender, EventArgs e) { if (ProcessEvent == null) ProcessEvent += new ProcessDelegate(t_ProcessEvent); ProcessEvent(sender, e); } //If you do not specify the associated method yourself, the method will be called and an error will be thrown. void t_ProcessEvent(object sender, EventArgs e) { throw new Exception("The method or operation is not implemented."); } void OnProcess() { ProcessAction(this, ); } public string Process() { OnProcess(); return s1 + s2; } } }
What did you feel? Is it similar to the code injection? It is equivalent to injecting any code that conforms to the delegate interface (the delegate is indeed very similar to the interface) into the Process process. Assign him a value before he returns.
3. Callback function
It’s so tired after typing so many words!
A callback function is to pass one method to another method for execution. There are many callback functions in C#, such as asynchronous operations. Here is an example:
using System; using ; using ; namespace TestApp { /// <summary> /// Entrust /// </summary> /// <param name="s1"></param> /// <param name="s2"></param> /// <returns></returns> public delegate string ProcessDelegate(string s1, string s2); class Program { static void Main(string[] args) { /* Call method */ Test t = new Test(); string r1 = ("Text1", "Text2", new ProcessDelegate(t.Process1)); string r2 = ("Text1", "Text2", new ProcessDelegate(t.Process2)); string r3 = ("Text1", "Text2", new ProcessDelegate(t.Process3)); (r1); (r2); (r3); } } public class Test { public string Process(string s1,string s2,ProcessDelegate process) { return process(s1, s2); } public string Process1(string s1, string s2) { return s1 + s2; } public string Process2(string s1, string s2) { return s1 + + s2; } public string Process3(string s1, string s2) { return s2 + s1; } } }
Output result:
Text1Text2
Text1
Text2
Text2Text1
The Process method calls a callback function, and of course only the callback function is executed here. It can be seen that any method that conforms to this delegation can be passed in, which means that this part of the code is variable. There is a principle of extracting variable parts of the code in the design, and this usage can undoubtedly be used in that occasion.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.