The use of C # proxies is primarily when you need to pass one method as a parameter to another method.
For example, start a thread to perform a task, and the method that the thread executes can be passed through the proxy.
A proxy consists of a method or multiple methods whose address is similar to a C + + function pointer, but it is type-safe.
1. Declaring an agent
delegate void Intmethodinvoker (int x);
This proxy can refer to the return value type is empty, there is a parameter for all methods, it is important to know that delegate is type safe.
Delegate Double Twolongsop (long first, long second);
There are two parameters that return a proxy with a double value.
Delegate string getastring ();
No arguments return a proxy with a value of string.
Public delegate string getastring ();
You can also describe the delegate's access rights.
2. Using proxies
The use of a proxy must first instantiate a proxy
Pri v a te delegate string getastring ();
static void Main ()
{
int x = 40;
getAString Firststringmethod = new getAString (x.tostring); Instantiate an agent and point the agent to X.tostring ();
Console.WriteLine ("String is {0}", Firststringmethod ()); Firststringmethod () becomes x.tostring () representative (agent)
With Firststringmethod initialized to X.tostring (),
The above statement is equivalent to saying
Console.WriteLine ("String is {0}", x.tostring ());
}
Not to be continued ...
C # Delegate of Reading notes