5

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?

George Duckett
  • 31,770
  • 9
  • 95
  • 162
  • If you are concerned about that, one solution would be not to use extension methods. `;)` You could inherit from the class you are extending and add the method that way. You will get warnings that you are hiding the new method that is added later. Or you could just remove the `this` parameter modifier from the static method. – Daniel A. White Jul 19 '11 at 12:29

3 Answers3

6

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.

Anders Abel
  • 67,989
  • 17
  • 150
  • 217
0

Use obscure method names that would never be used in the framework.

edit -- perhaps obscure wasn't the most appropriate word, please substitute with meaningful but less common verbage

Attempting to avoid signature conflicts is really the only strategy to avoiding the hassle of code rework (assuming the functionality of the extension method needs to be preserved and not simply converted to the framework's definition of the method).

roken
  • 3,946
  • 1
  • 19
  • 32
  • Agreed, they should be meaningful, but I don't think meaningful and obscure need to be mutually exclusive. Pull out the thesaurus and use less common but obvious verbs. – roken Jul 19 '11 at 12:37
  • 1
    If you're so worried you could always add a prefix, like ext_Redefine instead of Redefine. – Blam Jul 19 '11 at 12:47
0

I'm afraid that the only thing you can do is providing unique enough names to your extension methods so you're 100% sure you will never have a conflict.

Not talking about adding the name of your cat to the method's name, just try to be more creative :)

Claudio Redi
  • 67,454
  • 15
  • 130
  • 155