Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can a .NET delegate not be declared static?

When I try to compile the following:

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

I receive, as an error: "The modifer 'static' is not valid for the this item."

I'm implementing this within a singleton, with a separate class which calls the delegate. The problem is that when I use the singleton instance within the other class to call the delegate (from the identifier, not the type), I can't do that for whatever reason, even when I declare the delegate non-static. Obviously, I can only refer to it via the type directly if and only if the delegate is static.

What is the reasoning behind this? I am using MonoDevelop 2.4.2.

update

After trying 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've received a processing error, which states that the MoveMethod must be a type, and not an identifier.

like image 511
zeboidlund Avatar asked Jul 26 '11 19:07

zeboidlund


People also ask

Can a delegate be static?

Static delegates are not without limitations. They can only refer to static functions; member methods on objects are not permitted because there is no place to store the pointer to the object. Furthermore, static delegates cannot be chained to other delegates.

Can we declare delegate inside class C#?

In C# 3.0 and later, delegates can also be declared and instantiated by using a lambda expression, as shown in the following example. // Instantiate Del by using a lambda expression. Del del4 = name => { Console. WriteLine($"Notification received for: {name}"); };

Can we assign value to static variable in C#?

Static variables are used for defining constants because their values can be retrieved by invoking the class without creating an instance of it. Static variables can be initialized outside the member function or class definition. You can also initialize static variables inside the class definition.

How do you declare a delegate in C#?

A delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance.


1 Answers

Try this:

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

So the method-variable can be defined static. The keyword static has no meaning for the delegate definition, just like enum or const definitions.

An example of how to assign the static method-field:

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());    } } 
like image 183
Chaim Zonnenberg Avatar answered Oct 05 '22 18:10

Chaim Zonnenberg