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

后端 未结 7 1631
温柔的废话
温柔的废话 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条回答
  •  萌比男神i
    2020-11-27 06:55

    That's an easy one. The keyword 'this' returns a reference to the current instance of the class containing it. Static methods (or any static member) do not belong to a particular instance. They exist without creating an instance of the class. There is a much more in depth explanation of what static members are and why/when to use them in the MSDN docs.

提交回复
热议问题