Solutions C # Agent and Events (1)
Suddenly writing this article, it is really a bit, it is doing it, I think this is not what, it is difficult
What is understood, in fact, many people have written, and I have to be less than I write. But
Still think it is necessary to mention it. Because it is very interesting to understand the proxy and incident, it is
It is necessary. So well, let me talk about it, and there will be some examples.
First of all, I want to talk about the incident. The event as the event is of course the message received by the Windows program.
Then I will raise a few examples of the event, such as mouse movement. Press. It's so. So what?
Many people say that it looks up, just wants to be a hocked function pointer. I think this statement is very
Correct, truthful. On MSDN, we can often see the word "multiple broadcast" related to the agent.
I feel good, but I am not understood. But I will explain in detail below.
Agent: (some books are also translated into refers to referring to or commission. English is "delegate")
I think many people who have just contacted C # will be interested in agents. In fact, I don't know it, you can't help
To do a Windows program. The function pointer in the traditional sense is that the agent is a type in C #, so
It looks more secure, more in line with Oo spirit. The agent actually does is wrapped up by reference.
And make functions have a valid return value. I don't know if I say it is so understanding. So I will give an example, you will build a house.
Obviously, I am talking about what you do. Then the building is a proxy, it refers to it, what you have to do, but it
Did not do anything, in fact, in the work of building a house, you have done, then what is the result? Of course it is established
A house. Yes, the construction of the house is a proxy, and how to build a house is the work that the function should be completed. And what is the construction?
The house is returned. I still remember that I have said that the agent is a type? Hehe .. I think you should remember, because,
That is very novel, at least I think so. Ok, let's take a look at the namespace system.delagate, have seen it?
Agent class.
For example: public delegate void getString () // I declared a proxy
Now I have to use it; int i = 100; getString GS = new getString (i.tostring); // I now Int's Tostring method
In a proxy. It looks like to construct a function. This is often seen in the book "name is equivalent, and
Not structural equivalent ", I want to see you still don't understand. So, I will come again.
as follows:
FLOAT J = 0.0001;
GetString GS = New getString (j.totring); // See the Tostring method of int
The structure of the TString method of Float is different, but the return value of the name and type
The parameters are the same. Now, I think, you should understand it.
However, we often see in MSDN, such sentences. Single proxy and multi-channel broadcast. Look, it is a bit bad.
In fact, I started to see such a sentence, it is a bit of headache. So, I think that the example is the best explanation.
SINGLE DELEGATE: (Single Agent)
From the literal, we can understand this way, this agent is just a single agent working. So well, let
Let's take a look at how it works. I will define a proxy below:
Public Delegate Bool Myfun (String Str, INT i)
Now I will write a method as follows: BOOL CompareStrt (String S, INT I) {IF (S.comPareto (I.TOString ()) == 0) Return True; Else Return false;}
This method is very simple to work, just compare characters. So what is the relationship with the agent? Remember
What I said? The agent is in terms of verb. The code is as follows:
MyFun MF = New (CompareStrtOIN);
String s = "10000"; int i = 10000; console.writeline ("value =" mf (s, i)); output results:
Value = TRUE
This is a single proxy. It only agents. Well, maybe you want to see the complex example, more interesting in the back,
It is time to discuss multiple broadcasts.
Multi-channel broadcast:
A agent simultaneously proxy. It is what we have said before, you go to build a house, now it is not only
Build a home, but also build other buildings, etc., but they are in building a house, the parameters passing are also the same.
The type of return value is also the same as the house. So why don't we find an agent to complete such a task?
These things are finished by him, not to save us a lot of time and money. Yes we can do that
System.MulticastDelegate is actually in .NET Framework you can also find this class, multiple proxy
Translate into multiple broadcasts on MSDN. In fact it also overloads operators =. In fact, multiple broadcast and single proxy are in use.
The upper difference is not big. You can look at the example below.
Using system;
Namespace Multi_castdelegate {///
Namespace Multi_castdelegate {///
Public static string WritestRing (string s) {console.writeline ("Writing String"); return "null";}
Public static string logstring (string s) {console.writeLine ("Loging String"); return "null";}
Public static string transmitString (string s) {console.writeLine ("transmitting string"); return "null";}}}
The Main class: using System; using System.Threading; namespace Multi_castDelegate {///
Myclassdelegate.stringdelegate myden;
Writer = new MyClassDelegate.StringDelegate (MyClass.WriteString); /// calling Writer Writer ( "hello i am Writer just acting like Single cast"); Logger = new MyClassDelegate.StringDelegate (MyClass.logString); /// calling Logger Logger ( "hello i am Logger just acting like Single-cast"); Transmitter = new MyClassDelegate.StringDelegate (MyClass.TransmitString); /// calling Transmitter Transmitter ( "hello i am Transmitter just acting like Single-cast"); // / here mydelegate used the Combine method of System.MulticastDelegate /// and the delegates combine myDelegate = (MyClassDelegate.StringDelegate) System.Delegate.Combine (Writer, Logger); myDelegate ( "used Combine"); /// here Transmitter is also added using the overloaded form of Combine myDelegate = Transmitter; myDelegate ( "Using Overloaded form"); /// now using the Remove method myDelegate = (MyClassDelegate.StringDelegate) System.Delegate.R emove (myDelegate, Writer); myDelegate ( "Without Writer"); /// overloaded Remove myDelegate- = Transmitter; ( "Without Transmitter") myDelegate; System.Threading.Thread.Sleep (2300);}}} (above The example is found on a foreign website. I feel good, I will use it directly.)
The above example focuses on the two overloaded operators. "- =" and " =". By the above example, you can clear
See how multiple broadcasts can proxy multiple methods. Of course, you can also delete those "- =" operations you don't want.
The inquiry can be. (Then I will discuss events in the next article)