Why keyword 'this' cannot be used in a static method?

后端 未结 13 1517
遇见更好的自我
遇见更好的自我 2020-12-16 14:13

Why can\'t the keyword this be used in a static method? I am wondering why C# defines this constraint. What benefits can be gained by this constraint?

[

13条回答
  •  醉梦人生
    2020-12-16 14:50

    The this keyword can be used in a method marked as static. The syntax is used to define extension methods in C#. This feature has been available since C# 3.0, released in 2007 (Wikipedia)

    In the normal usage, this refers to the instance, static says that there is no instance (and therefore no this). The fact that you can't use them together (aside from special exceptions like extension methods) follows naturally from understanding what this and static are, conceptually.

提交回复
热议问题