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

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

    I am not sure if this helps your problem, but I believe these two code snippets are equivalent:

    MyStaticClass.foo();
    

    and simply

    foo();
    

    will both call the foo() method in the MyStaticClass class, assuming you call foo() from inside MyStaticClass

    Edit - The easiest way to remember the difference between a static class and a non-static class is to think of something like the Math class in java. You can call Math.abs(x); to get the absolute value of x, and it does not really make sense to instantiate a Math object, which is why Math is a static class.

提交回复
热议问题