Partial Class vs Extension Method

前端 未结 9 1670
無奈伤痛
無奈伤痛 2021-02-01 02:09

I dont have much experience of using these 2 ways to extend a class or create extension methods against a class. By looking others work, I have a question here.

I saw pe

9条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-01 02:19

    Partial classes should be used in code generation scenarios.

    Since the generated file might get overwritten at any time, one uses partial classes to write into the non-generated file.

    Additionally, partials will only work if they are part of the same assembly - they cannot cross assembly boundaries.

    If these are not your constraints, you can and should use extension methods - of course, after considering other possibilities such as inheritance and composition for suitability.

提交回复
热议问题