A contractor where I work is using extension methods to implement CRUD on well-known internal classes that we own. I say it is better to use normal inheritance over extension methods for the following reasons.
- Using extension methods obfuscates, hides & confuses the source of the
CRUDmethods. - I assume
extension methodsmake heavy use ofreflection(which is slower).
His logic is, “It’s compiled, so it’s fast.” Maybe I’m wrong…but just because it is compiled doesn’t mean it doesn’t use reflection, nor does it mean it is faster than normal inheritance.
So my questions are:
- How do
extension methodswork under-the-hood? - Is it better to use
inheritanceorextension methodson WELL-KNOWN classes that you OWN?
They’re just static methods; the compiler rewrites calls like
myObject.MyExtensionMethod()toMyExtensionClass.MyExtensionMethod(myObject).There’s not single answer to this question, it all depends on the context. But usually extension methods are most useful in those cases:
IEnumerable<T>and Linq extension methods)