ReSharper complains when method can be static, but isn't

前端 未结 8 1350
萌比男神i
萌比男神i 2020-11-28 08:43

Why does ReSharper complain when a method can become static, but is not?

Is it because only one instance of a static method is created (on the type) and thus save on

相关标签:
8条回答
  • 2020-11-28 09:13

    From the FxCop documentation for the same warning (emphasis added):

    "Members that do not access instance data or call instance methods can be marked as static (Shared in Visual Basic). After you mark the methods as static, the compiler will emit non-virtual call sites to these members. Emitting non-virtual call sites will prevent a check at runtime for each call that ensures that the current object pointer is non-null. This can result in a measurable performance gain for performance-sensitive code. In some cases, the failure to access the current object instance represents a correctness issue."

    0 讨论(0)
  • 2020-11-28 09:14

    You don't have to push "this" onto the function's stack for a static method. That's another reason it's cheaper.

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