2 Again, these rules act just as if
you were calling the method directly.
NOTE Utter garbage! (Or not, as the case may be??¦)??”It??™s worth being aware that a
delegate instance will prevent its target from being garbage collected, if
the delegate instance itself can??™t be collected. This can result in apparent
memory leaks, particularly when a ???short-lived??? object subscribes to
an event in a ???long-lived??? object, using itself as the target. The long-lived
object indirectly holds a reference to the short-lived one, prolonging
its lifetime.
There??™s not much point in creating a delegate instance if it doesn??™t get invoked at
some point. Let??™s look at our last step??”the invocation.
INVOKING A DELEGATE INSTANCE
This is the really easy bit3??”it??™s just a case of calling a method on the delegate instance.
The method itself is called Invoke, and it??™s always present in a delegate type with the
same list of parameters and return type that the delegate type declaration specifies. So
in our case, there??™s a method like this:
void Invoke (string input)
Calling Invoke will execute the action of the delegate
instance, passing on whatever parameters you??™ve
specified in the call to Invoke, and (if the return type
isn??™t void) returning the return value of the action.
Simple as this is, C# makes it even easier??”if you
have a variable4 whose type is a delegate type, you can
treat it as if it were a method itself.
Pages:
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102