Say I have an overloaded extension method with the following two signatures:
public static void MyExtensionMethod(this Foo foo);
public static void MyExtensionMethod(this Foo foo, Bar bar);
I’d like to chain one method to the other. I can do this in one of two ways:
Chaining Technique #1
public static void MyExtensionMethod(this Foo foo)
{
// Call overload using extension method syntax.
foo.MyExtensionMethod(new Bar());
}
public static void MyExtensionMethod(this Foo foo, Bar bar)
{
// Do stuff...
}
Chaining Technique #2
public static void MyExtensionMethod(this Foo foo)
{
// Call overload as a regular method.
MyExtensionMethod(foo, new Bar());
}
public static void MyExtensionMethod(this Foo foo, Bar bar)
{
// Do stuff...
}
This is my question: Is there any difference between calling the overloaded method as an extension method versus as a regular method? If so, what’s the difference? Is one preferable to the other?
The second one is actually more stable. The first one will be changed if I do something like:
If I add this then in the first case it calls the instance method, and in the second case it calls the extension method.