From what I have read about extension methods, you will run into problems if the base class decides to add a method with the same name as your extension. For a specific class it is generally not to hard to pick a name to avoid clashes, however extension methods can be added to interfaces which adds infinitely more potential for conflicts.
In Objective-C (with their version, categories), this problem is avoided by adding a prefix before each method. I know we can define extension methods in namespaces so that we can control whether they are imported or not, but this only solves the problem of clashes between extension methods, rather than clashes between an extension method and the base class.
Update:
Nobody, actually mentioned this, but extension methods aren't virtual. That means if you can i.myExtension() on an interface i, then it will always call the interface classes method, so the subclass method (which could have different intent) won't be called. So, overall, using extension methods is quite safe.
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.
An extension method must be a static method. An extension method must be inside a static class -- the class can have any name. The parameter in an extension method should always have the "this" keyword preceding the type on which the method needs to be called.
Extension methods are an excellent addition to the C# language. They enable us to write nicer, more readable code. They allow for more functionally styled programming, which is very much needed in an object-oriented language. They also should be used with care.
The use of a prefix will make the code ugly, which removes some of the value of an extension method.
I recommend instead, being careful to not create a large number of extension methods and to use method names that make sense. This way, in the case of a conflict, it's more likely that, for instance, the extension Where
method and the conflicting Where
method will have similar semantics.
The common convention is to have them in their own separate namespace (usually in the form of <something>.Extensions
) which you can then use to decide whether or not you use them in any particular piece of code which contains types the extensions normally operate on.
Furthermore, if you do have a conflict, remember that extension methods are still static methods, so you can always call them explicitly in the case of a name conflict.
However, if you find that you are frequently running into name conflicts, you might want to reconsider the names you are choosing so that they don't interfere with names that already exist on the type.
To summarize, no, you should not prefix extension method names in C#, as they only serve to obfuscate your code and make it harder to maintain.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With