【发布时间】:2011-09-16 09:57:55
【问题描述】:
如果我使用仅在方法中需要的常量,最好是在方法范围内还是在类范围内声明该常量?在方法中声明它是否有更好的性能?如果这是真的,我认为在类范围(文件顶部)定义它们以更改值并更容易重新编译是更标准的。
public class Bob
{
private const int SomeConst = 100; // declare it here?
public void MyMethod()
{
const int SomeConst = 100; // or declare it here?
// Do something with SomeConst
}
}
【问题讨论】: