0

Save for tradition, is there a reason why I would want to use

void foo(int k) {
    // stuff
}

over

Action<int> foo = new Action 
    ((k) => { 
        // stuff
    });

More generally, are there any practical advantages to defining my functions normally instead of defining them as delegates?

Damien
  • 263
  • 1
  • 4
  • 15
  • you mean aside from the saner syntax and the fact that it will be an function instead of an delegate with overhead? No ... – Random Dev May 06 '15 at 17:18
  • According to Jon Skeet delegates can be faster, at least when using the method as the target of a callback. http://stackoverflow.com/questions/2082735/performance-of-calling-delegates-vs-methods – Eric J. May 06 '15 at 17:21
  • as interface-methods ... also: *so what*? - You think it's a good idea to take in the bad javascript parts into C#? – Random Dev May 06 '15 at 17:23
  • 1
    There are a couple perf considerations. Creating a delegate is an additional allocation. Another is that delegate invocations can't be inlined effectively. – Mike Zboray May 06 '15 at 17:23
  • Syntax and overhead are two separate arguments. I don't see the overhead aspect as being at all relevant 99.999% of the time (whether slightly slower or slightly faster, it should usually not matter). I agree with you on the syntax aspect. – Eric J. May 06 '15 at 17:25
  • @mikez What do you mean that they can't be in lined effectively? – Damien May 06 '15 at 17:28
  • Another performance consideration would be lifetime extension of any variables captured by the delegate (also another class is allocated to hoist the variables apart from the delegate itself). – Mike Zboray May 06 '15 at 17:33
  • @Damien If foo is a method when it is invoked the JIT can decide to inline the machine code into the caller. If foo is a delegate it can't really do that because it doesn't know what is being called until it looks up whatever methods (yes, methods because a delegate has an invocation list) were being pointed to. – Mike Zboray May 06 '15 at 17:35
  • 2
    @Damien See [this article](http://blogs.msdn.com/b/ericgu/archive/2004/03/19/92911.aspx) for a discussion of the effect of indirection (virtual/interface/delegate dispatch) on inlining. – Mike Zboray May 06 '15 at 17:40

1 Answers1

2

Sure, there are plenty of good reasons. The first three to come to mind:

Pretty much any C# programmer will be highly surprised by your extremely uncommon overuse of delegates.

Functions can be virtual, and can be used to implement interfaces.

Variables, even variables of delegate type, can be re-assigned, which would lead to highly unexpected behaviour.

But there are plenty more.

Eric Lippert mentions in a comment that functions can be overloaded, variables cannot.

Methods can be implemented as iterators (using the yield keyword), anonymous functions cannot.

Some special methods can be called implicitly: new T { a, b, c } looks for a method named Add, not a variable named Add.

The predefined delegate types do not allow you to use ref or out parameters.

The predefined delegate types do not allow you to add attributes to the return type or parameters.