Why can't a .NET delegate be declared static?

When I try to compile the following:

public static delegate void MoveDelegate (Actor sender, MoveDirection args); 

I get as an error: "The static modifier is not valid for this element."

I implement this in singleton mode, with a separate class that invokes the delegate. The problem is that when I use a singleton instance in another class to call a delegate (from an identifier, not from a type), I cannot do this for any reason, even when I declare a delegate as non-stationary. Obviously, I can only refer to it through a type, if and only if the delegate is static.

What is the reason for this? I am using MonoDevelop 2.4.2.

Update

Try one of the suggestions with the following code:

 public void Move(MoveDirection moveDir) { ProcessMove(moveDir); } public void ProcessMove(MoveDirection moveDir) { Teleporter.MoveMethod mm = new Teleporter.MoveMethod(Move); moveDelegate(this, moveDir); } 

I got a processing error that says MoveMethod should be a type, not an identifier.

+16
c # compiler-errors delegates
Jul 26 '11 at 19:55
source share
6 answers

Try the following:

 public delegate void MoveDelegate(object o); public static MoveDelegate MoveMethod; 

Thus, a method variable can be defined as static. The static does not make sense for a delegate definition, like an enum or const definition.

An example of how to assign a static field to a method:

 public class A { public delegate void MoveDelegate(object o); public static MoveDelegate MoveMethod; } public class B { public static void MoveIt(object o) { // Do something } } public class C { public void Assign() { A.MoveMethod = B.MoveIt; } public void DoSomething() { if (A.MoveMethod!=null) A.MoveMethod(new object()); } } 
+25
Jul 26 '11 at 19:58
source share
β€” -

You declare a delegate type. It makes no sense to declare it as static . You could declare an instance of your delegate type as static , however.

 public delegate void BoringDelegate(); internal class Bar { public static BoringDelegate NoOp; static Bar() { NoOp = () => { }; } } 
+7
Jul 26 '11 at 19:58
source share

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() { // get a reference to the Move method _myMoveDelegate = Move; // or, alternatively the longer version: // _myMoveDelegate = new MoveDelegate(Move); // works for static methods too _myMoveDelegate = MoveStatic; // and then simply call the Move method indirectly _myMoveDelegate(someActor, someDirection); } 

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).

+5
Jul 26 '11 at 20:47
source share

A delegate declaration is a type declaration. It cannot be static, just as you cannot define a static enumeration or structure.

However, I prefer to use the interface instead of the raw delegate .

Consider this:

 public interface IGameStrategy { void Move(Actor actor, MoveDirection direction); } public class ConsoleGameStrategy : IGameStrategy { public void Move(Actor actor, MoveDirection direction) { // basic console implementation Console.WriteLine("{0} moved {1}", actor.Name, direction); } } public class Actor { private IGameStrategy strategy; // hold a reference to strategy public string Name { get; set; } public Actor(IGameStrategy strategy) { this.strategy = strategy; } public void RunForrestRun() { // whenever I want to move this actor, I may call strategy.Move() method for (int i = 0; i < 10; i++) strategy.Move(this, MoveDirection.Forward); } } 

In the call code:

 var strategy = new ConsoleGameStrategy(); // when creating Actors, specify the strategy you want to use var actor = new Actor(strategy) { Name = "Forrest Gump" }; actor.RunForrestRun(); // will write to console 

This is similar to the spirit of the development strategy template and allows you to separate Actor from the actual implementation strategy (console, graphical, no difference). Subsequently, other strategies may be required that make him a better choice than a delegate.

Finally, you can use the control inversion structure to automatically enter the correct instance of the strategy in your Actor classes, so there is no need for manual initialization.

+2
Jul 26 '11 at 20:08
source share

define your delegate, in your static class declare an instance variable for it.

 public delegate void MoveDelegate (Actor sender, MoveDirection args); public static MyClass { public static MoveDelegate MoveDelegateInstance; } 
0
Jul 26 '11 at 19:58
source share
 public static delegate void MoveDelegate (Actor sender, MoveDirection args); 

Let me tell you what happened when you announced the delegate

The compiler creates a class in this case called MoveDelegate and extends it using System.MulticastDelegate .

Since you cannot extend any non-static type to a static type.

So this is why the compiler does not allow static delegate declaration. But still, you might have a delegate static link.

-one
Sep 20 '16 at 10:29
source share



All Articles