假设您有一些可以在非静态类中设为静态的方法。
例如:
private double power(double a, double b)
{
return (Math.Pow(a, b));
}
您认为将方法签名更改为静态有什么好处吗?在上面的例子中:
private static double power(double a, double b)
{
return (Math.Pow(a, b));
}
即使有一些性能或内存增益,编译器不会将其作为编译时的简单优化吗?
Edit: What I am looking for are the benefits by declaring the method as static. I
know that this is the common practice. I would like to understand the logic behind it.
And of course, this method is just an example to clarify my intention.
根据定义,power
是无状态的,并且对任何封闭类没有副作用,因此应该声明它static
.
This article http://msdn.microsoft.com/en-us/library/ms973852.aspxMSDN 讨论了非静态与静态的一些性能差异。该调用大约比实例化和调用快四倍,但它实际上只在作为性能瓶颈的紧密循环中才重要。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)