Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What makes a name of a method equivalent to an action delegate?

Tags:

c#

delegates

I was just experimenting and ended up with the following snippet:

public static class Flow {
    public static void Sequence(params Action[] steps) {
        foreach (var step in steps)
            step();
    }
}
void Main() {
    Flow.Sequence(() => F1(), () => F2());
    Flow.Sequence(F1, F2); // <-- what makes this equiv to the line above?

}
void F1() { }
void F2() { }

I didn't realize that a method name alone was the same as an Action.

What is making this so?

like image 559
Aaron Anodide Avatar asked Nov 12 '11 02:11

Aaron Anodide


People also ask

What is difference between delegate and method?

A delegate is a method with a parameter and a return type. A delegate is a type that safely encapsulates a method. Delegates are object-oriented, type safe, and secure.

What is a delegate method?

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.

What is the difference between action and Func delegate?

An Action type delegate is the same as Func delegate except that the Action delegate doesn't return a value. In other words, an Action delegate can be used with a method that has a void return type. It can contain minimum 1 and maximum of 16 input parameters and does not contain any output parameter.

How do you call an Action delegate in C#?

You can initialize an Action delegate using the new keyword or by directly assigning a method: Action<int> printActionDel = ConsolePrint; //Or Action<int> printActionDel = new Action<int>(ConsolePrint); An Action delegate can take up to 16 input parameters of different types.


1 Answers

In C#, delegates are nothing more than method pointers. They can point to existing methods in a class, or independent anonymous delegate objects altogether.

This paragraph from the above link should explain what's happening in your code:

Any method that matches the delegate's signature, which consists of the return type and parameters, can be assigned to the delegate. This makes is possible to programmatically change method calls, and also plug new code into existing classes. As long as you know the delegate's signature, you can assign your own delegated method.

That is, when resolving delegate types, what's considered are their signatures, rather than their names.

In your case, your F1() and F2() methods, taking no parameters and returning nothing, have matching signatures with the parameterless Action delegate:

public delegate void Action();

Therefore, they're implicitly convertible to Action.

If you try to pass a method with a different return type or at least one parameter, you'll get a compile-time error as it won't correspond to Action's signature.

like image 54
BoltClock Avatar answered Sep 26 '22 21:09

BoltClock