What is the difference between an extension method and a static method?

What is the difference between an extension method and a static method?

I have two classes:

public static class AClass { public static int AMethod(string ....) { } } 

and

 public static class BClass { public static int BMethod(this string ....) { } } 

I can use them as

 AClass.AMethod('...'); 

or

 '...'.BMethod(); 

What is being offered?

+6
source share
2 answers

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.

+27
source

You cannot override the extension method. Only if the method has a different signature, then it can be overloaded.

Disabling a course has some limitations: Extension methods must be implemented as static methods and static classes (more precisely, inside a non-nested, nonequivalent static class). You can use extension methods to extend a class or interface, but not to override them. An extension method with the same name and signature as an interface or class method will never be called. At compile time, extension methods always take precedence over instance methods defined in the type itself. Extension methods cannot access private variables in the type that they distribute. You can consider extension methods as a “legitimate” way to add more static methods to existing classes without actually inheriting them. But the funny thing is that unlike ordinary static methods of a class, you cannot call extension methods at the class level (you will get a compile-time error if you try this), but instead you must call them on an instance of the class (as if they were the usual instance methods of this class, and they weren’t !!!).

In addition, inside the extension method, you can freely use the public properties of the instance of the past object in which the method is called; you are in no way limited only to the data of the static object. Only the extension method is a static method, but the object to which it is called is a complete, ordinary instance of the object.

+1
source

Source: https://habr.com/ru/post/955119/


All Articles