Using Extensions: Weighing The Pros vs Cons

前端 未结 5 1162
时光取名叫无心
时光取名叫无心 2021-01-06 06:54

Recently I asked a question about how to clean up what I considered ugly code. One recommendation was to create an Extension Method that would perform the desired function

5条回答
  •  长情又很酷
    2021-01-06 07:46

    [OP] Definetely a handy way to go, but is it really worth the overhead of another class object?

    No extra class object is created in this scenario. Under the hood, extension methods are called no differently than a static method. There is an extra metadata entry for the extension method container but that is pretty minimal.

    [OP] And what happens if someone wants to reuse my code snippet but doesn't understand Extension Objects?

    Then it would be a good time to educate them :). Yes, there is the risk that a new developer may not be comfortable with extension methods to start. But this is hardly an isolated feature. It's being used more and more in all of the code samples I'm seeing internally and on the web. It's something that is definitely worth while for a developer to learn. I don't think it fits into the category of "to esoteric to expect people to know"

提交回复
热议问题