Static vs. non-static method

前端 未结 10 1155
执念已碎
执念已碎 2020-11-28 08:30

Suppose you have some method that could be made static, inside a non-static class.
For example:

private double power(double a, double b)
    {
        r         


        
10条回答
  •  情书的邮戳
    2020-11-28 09:11

    As defined, power is stateless and has no side effects on any enclosing class so it should be declared static.

    This article from MSDN goes into some of the performance differences of non-static versus static. The call is about four times faster than instantiating and calling, but it really only matters in a tight loop that is a performance bottleneck.

提交回复
热议问题