Search code examples
c#delegatesanonymous-methods

What it means to have body in delegates declaration?


I have just seen following code:

class X
{
  static Action Ac()
  {
     return ..some other code
  }
}

What does it mean? I have never seen a delegate with its body declared.


Solution

  • That's not an Action delegate with its body declared. That's a static method of the X class called Ac(), with a return type of Action; that is, it's a class method that returns an Action delegate. The body presumably creates an Action object to return from the method.

    To put it another way: it is a regular static method, which happens to return Action instead of something like string or int.