0

When using recursion in an extension method, there are two different ways to invoke the method from within itself.

Option 1: As an extension method

internal static class MyExtension { public static object DoThing(this MyType source, MyOtherType parameter) { // ... some stuff ... return source.DoThing(parameter.Property); } } 

Option 2: As a static method

internal static class MyExtension { public static object DoThing(this MyType source, MyOtherType parameter) { // ... some stuff ... return DoThing(source, parameter.Property); } } 

What would be the trade-offs of these options? I can imagine that there might be slight differences in performance, the possibility of the signature getting overwritten at a later date, etc.

1
  • 2
    "I can imagine that there might be slight differences in performance" - what about using ildasm and comparing the IL code? Commented Jul 11 at 12:59

2 Answers 2

12

The two are exactly the same. The extension method syntax is just a sugar to make calling methods nicer.

Internally, compiler will compile both to exactly same method call. There is no impact of extension method being called in recursion. It is same as non-recursive call.

1

Be consistent. If a method is defined as an extension method then it should always be invoked as an extension method. Invoking it sometimes as an extension method and sometimes as a regular static would just be confusing for no purpose.

There is no performance difference since extension methods are resolved to static method invocations at compile time.

In theory there is a risk that an extension method invocation can silently change to a different implementation, e.g. if an instance method with the same signature is added directly to MyType. But this issue is not specific to recursive calls, so if this is a worry you should not define the method as an extension method at all.

4
  • At compile time? Would there be a difference if a method is in another assembly? The method would not be resolved until runtime/linktime. A static member of the same class could be inlined at compile time, while a method from another assembly would not. I guess it is more a question of method locztion than type... Commented Jul 11 at 14:06
  • 1
    @Basilevs There is no runtime difference between invoking as static and invoking as extension method. It literally compiles to the same code. Commented Jul 11 at 17:14
  • @Basilevs - there is probably a measurable difference in compile time, but probably not noticeable at human scales. Extension methods are syntactic sugar that the compiler translates. If I remember correctly, one of the first passes the C# compiler does is translate syntactic sugar to other kinds of API calls which are harder to read. I haven't looked into this in quite a few years, though, so my memory is fuzzy (1/2). Commented Jul 17 at 19:16
  • 1
    (2/2) Regardless, this is premature optimization taken to an absurd extreme, even by developer standards. We've spent more time discussing this question than all of humanity will save in compile time choosing to call methods statically for all the time that C# is a language we use. But then again, aren't these are the fun conversations to have? :) Commented Jul 17 at 19:17

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.