The delegate declaration basically declares a method signature, which includes only information about its parameters and return type. And since the same delegate can point to both static and instance methods, it makes no sense to force an old or instance of the method signature itself.
Once you have declared your delegate as:
public delegate void MoveDelegate (Actor sender, MoveDirection args);
this means that any delegate of this type must point to a method that takes one Actor parameter, one MoveDirection parameter and returns void , regardless of whether the method is static or an instance. You can declare a delegate in the namespace area or inside the class (just as you declare a nested class).
So, after declaring MoveDelegate somewhere, you can create fields and variables of this type:
private MoveDelegate _myMoveDelegate;
and remember that the method must have a compliance signature:
// parameters and return type must match! public void Move(Actor actor, MoveDirection moveDir) { ProcessMove (moveDir); } public static void MoveStatic(Actor actor, MoveDirection moveDir) { ProcessMove (moveDir); }
then you can assign this method to a delegate elsewhere:
private void SomeOtherMethod() {
Itβs useful to know that .NET (starting with version v3.5) provides some predefined common delegates ( Action and Func ) that you can use instead of declaring your own delegates :
// you can simply use the Action delegate to declare the // method which accepts these same parameters private Action<Actor, MoveDirection> _myMoveDelegate;
Using these delegates is IMHO more readable, since you can immediately determine the signature of parameters from viewing the delegate itself (while in your case you need to look for an ad).
Groo Jul 26 '11 at 20:47 2011-07-26 20:47
source share