private const int THE_ANSWER = 42;
Run Code Online (Sandbox Code Playgroud)
要么
private const int theAnswer = 42;
Run Code Online (Sandbox Code Playgroud)
我个人认为在现代IDE中我们应该使用camelCase,因为ALL_CAPS看起来很奇怪.你怎么看?
如果我使用仅在方法中需要的常量,最好在方法范围内或类范围内声明const吗?是否有更好的性能在方法中声明它?如果这是真的,我认为在类范围(文件顶部)定义它们以更改值并更容易重新编译更为标准.
public class Bob
{
private const int SomeConst = 100; // declare it here?
public void MyMethod()
{
const int SomeConst = 100; // or declare it here?
// Do soemthing with SomeConst
}
}
Run Code Online (Sandbox Code Playgroud)