Why can a .NET delegate not be declared static?
You are declaring a delegate
type. It doesn't make any sense to declare it as static
. You could declare an instance of your delegate
type as static
, though.
public delegate void BoringDelegate();
internal class Bar {
public static BoringDelegate NoOp;
static Bar() {
NoOp = () => { };
}
}
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());
}
}