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

后端 未结 13 1520
遇见更好的自我
遇见更好的自我 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:54

    this is used to refer to the parent object of a variable or method. When you declare static on a method the method can be called without needing to instantiate an object of the class. Therefore the this keyword is not allowed because your static method is not associated with any objects.

    0 讨论(0)
提交回复
热议问题