Current location - Quotes Website - Signature design - The problem of delegate's example in msdn
The problem of delegate's example in msdn
1. the parameters declared by mydelegate are not the parameters for constructing the delegate instance, but the signatures of the methods that this delaget can be passed in. The simple understanding is that the definition part of delegate defines a signature, and only by using the methods of input and output types that meet this definition can an instance of delegate be constructed.

for example, the following proxy

delegate string say hello (string name, bool is happy) is defined;

Then there are only two input parameters, a string and a Boolean; Only methods whose output parameter is string can be used to construct SayHello.

For example, there are two methods

StringM1 (StringName, Boolis Happy)

StringM2 (StringName)

to execute new MyDelegate(m1); OK, execute new MyDelegate(m2); Will report an error.

2. The constructor of delegate passes in a pointer to the method, so it receives p.InstanceMethod instead of p.InstanceMethod (). D () is equivalent to d.Invoke ();

d.Invoke () is recommended; Writing instead of d (); Writing, because the former is more readable.