Why can't methods work syntactically as func / action delegates?

On the way home, I had an idea: create Func / Action extensions that would allow some syntactic sugar in C #.

A theoretical example ... create an extension for the various Func / Action permutations that allow you to run the method.

When I returned home and tried the example, I found that this was not possible. I believe this is a flaw / inconsistency in C #. Delegates and methods are the same (theoretically).

public static class Extensions { public static void Time(this Action action) { // Logic to time the action action(); } } public class Example { public void Main() { Action action = RunApp; Action actionLambda = () => { }; Action actionDelegate = delegate () { }; Extensions.Time(RunApp); // Works Extensions.Time(() => { }); // Works Extensions.Time(delegate() { }); // Works Extensions.Time(action); // Works Extensions.Time(actionLambda); // Works Extensions.Time(actionDelegate); // Works action.Time(); // Works actionLambda.Time(); // Works actionDelegate.Time(); // Works ((Action) RunApp).Time(); // Works ((Action) delegate () { }).Time(); // Works ((Action) (() => { })).Time(); // Works // These should all be the same! RunApp.Time(); // No good: "Example.RunApp() is a method which is not valid in the given context" () => { }.Time(); // No good: Operator '.' cannot be applied to operand of type 'lambda expression'" (() => { }).Time(); // No good: Operator '.' cannot be applied to operand of type 'lambda expression'" delegate() { }.Time(); // No good: "Operator '.' cannot be applied operand of the type 'anonymous method'" } public void RunApp() { // Stuff... } } 

I understand that Func / Action is a new addition to C # compared to delegates and method groups, but why can they not act the same?

+6
source share
1 answer

The action simply beautifully encapsulates delegates in a simple way without explicitly declaring your delegates. In C #, a delegate is a language engine for capturing a method as a type. Type is what you need to create an extension method on ... so, simplifying back to delegates ...

  public static class Extensions { public delegate void Del(); public static void Time(this Del action) { // Logic to time the action action(); } } class Program { static void Main(string[] args) { ((Extensions.Del)(() => { })).Time(); } } 

the language will not automatically drop methods in Del, you need to explicitly do this. Methods are not a type in themselves; delegates are a mechanism for capturing them. In fact, in C #, the delegate does not match the method. This is a C # solution for creating types of method pointers.

Another thing to keep in mind: you can: -

  public static class Extensions { public delegate void Del(); public delegate void Del2(); public static void Time(this Del action) { // Logic to time the action action(); } public static void Time(this Del2 action) { // Logic to time the action action(); } public static void Time(this Action action) { // Logic to time the action action(); } } class Program { static void Main(string[] args) { ((Extensions.Del)(() => { })).Time(); ((Extensions.Del2)(() => { })).Time(); ((Action)(() => { })).Time(); (() => { })).Time(); // no way to know what type this should be coerced to } } 

Now your methods can be captured as many different types.

0
source

All Articles