Why can't you use the keyword 'this' in a static method in .Net?

后端 未结 7 1610
温柔的废话
温柔的废话 2020-11-27 06:46

I\'m trying to use the this keyword in a static method, but the compiler won\'t allow me to use it.

Why not?

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 07:15

    Static methods are Class specific and not instance specific. "this" represents an instance of the class at runtime, so this can't be used in a static context because it won't be referencing any instance. Instead the class's name should be used and you would only be able to access static members in the class

提交回复
热议问题