Usage of Extension Methods

前端 未结 9 1690
抹茶落季
抹茶落季 2020-12-11 01:51

  • When does using extension methods make sense?
  • Does adding extension methods to a type affect performance?

    These questions are follow up to the question i a

  • 9条回答
    •  青春惊慌失措
      2020-12-11 02:18

      Addressing your second question: my rule of thumb is that the extension method should be a "natural extension of functionality" on the type or that it should be a maintainable and readable part of a fluent interface.

      An example of a "natural extension of functionality" is extending a data reader to return a default value if a DBNull is enountered. Not so natural would be extending data reader to return an instance of the entity represented by the data in multiple fields. In the latter case, you're injecting the wrong responsibilities into the poor object :).

    提交回复
    热议问题