How do extension methods work under-the-hood?

后端 未结 6 1112
半阙折子戏
半阙折子戏 2021-02-13 12:56

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

6条回答
  •  滥情空心
    2021-02-13 13:26

    How do extension methods work under-the-hood?

    They're just static methods; the compiler rewrites calls like myObject.MyExtensionMethod() to MyExtensionClass.MyExtensionMethod(myObject).

    Is it better to use inheretance or extension methods on WELL-KNOWN classes that you OWN?

    There's not single answer to this question, it all depends on the context. But usually extension methods are most useful in those cases:

    • you don't own the code for the extended type
    • the method targets an interface and will be the same for all implementations of this interface (e.g. IEnumerable and Linq extension methods)

提交回复
热议问题