The extension method is still a static method. You can use it just like you would use a regular static method.
The only difference is that the extension method allows you to use the method in a way that looks like part of this type, so you can write:
int result = stringValue.BMethod();
Instead:
int result = BClass.BMethod(stringValue);
This works exclusively as a compilation "trick" - the compiler sees the first form, and if BClass can be used (it has the correct using and is in the assembly referenced), then it will turn it into the second IL method for you. It is just a convenience.
What is being offered?
It really depends. If you are going to control a type, I would recommend putting methods in that type. This is usually more convenient.
If you are not controlling the type or trying to “extend” the generic type (for example, IEnumerable<T> ), extension methods may be reasonable.
However, if a type is a very common type, I usually avoid extension methods as they become “noise” in intellisense, which in turn can cause additional confusion. For example, I personally would not recommend adding extension methods to System.Object or System.String , etc.
source share