Why are “static” and “this” needed for extension methods, and how is their memory allocated?

后端 未结 5 1221
醉话见心
醉话见心 2020-12-10 05:47

A few questions about Extension Methods:

  1. Why are Extension Methods static?

  2. Why do they need to be declared in a static class?

  3. <
5条回答
  •  伪装坚强ぢ
    2020-12-10 06:23

    1. Because there is no instance to call them on.

    2. I think its to limit number of possible errors if it was declared in non-static class.

    3. It says its extension method. Compiler sees this and allows to use it on type declared as this, instead as static method.

    4. Extension method call is only syntactic sugar. This call gets converted into ordinary static method call in IL.

提交回复
热议问题