On the way home I had an idea: create Func/Action extensions which would allow some nice syntactic sugar in c#.
Theoretical example... create an extension for various permutations of Func/Action which allow you to time the method's execution.
As I arrived home and tried an example, I found this is not possibly. I believe it is a shortcoming/inconsistency in c#. Delegates and methods are one in the same (in theory).
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 Func/Action are newer additions to c# compared to delegates and method groups, but why can they all not act the same?