Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extension Methods forward compatible

With extension methods we can easily add methods to any type. Obviously this opens the possibility in a future version of .net the extension method could no longer get called (for example the type now includes a method with identical signature to the extension method).

Should this be a concern?

If so, how should I deal with this and design my extension methods as to minimise code changes should this happen?

like image 395
George Duckett Avatar asked Jul 19 '11 12:07

George Duckett


People also ask

What is an advantage of using extension methods?

The main advantage of the extension method is to add new methods in the existing class without using inheritance. You can add new methods in the existing class without modifying the source code of the existing class. It can also work with sealed class.

What is correct extension method?

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.

What are extension methods explain with an example?

An extension method is actually a special kind of static method defined in a static class. To define an extension method, first of all, define a static class. For example, we have created an IntExtensions class under the ExtensionMethods namespace in the following example.

Are extension methods good?

Adding extension methods to any type is a great way to improve productivity and simplify code.


1 Answers

If the framework is changed so much in the future, there will always be compatibility issues. If a new framework method is added with the same name as your extension method, it is quite likely that they have the same, or at least very similar functionality and a refactoring is due anyways.

I think that the power of the extension methods is too large to ignore just because of this risk.

like image 120
Anders Abel Avatar answered Oct 09 '22 14:10

Anders Abel