在尝试编译我的类时,我收到一个错误:
常量
'NamespaceName.ClassName.CONST_NAME'不能标记为静态.
在线:
public static const string CONST_NAME = "blah";
Run Code Online (Sandbox Code Playgroud)
我可以用Java一直这样做.我究竟做错了什么?为什么不让我这样做?
鉴于以下代码,我想知道为什么在编译失败时referenceValue = ConstantInt;有效referenceValue = StaticInt;.
namespace Demo
{
public class Class1
{
private const int ConstantInt = 42;
private static readonly int StaticInt = 42;
public void DemoMethod(ref uint referenceValue)
{
referenceValue = ConstantInt; // This compiles
referenceValue = StaticInt; // This claims that the source type 'int' cannot be converted to 'unit' implicitly.
}
}
}
Run Code Online (Sandbox Code Playgroud) 在声明全局变量时,"static"和"const"之间有什么区别;
namespace General
{
public static class Globals
{
public const double GMinimum = 1e-1;
public const double GMaximum = 1e+1;
}
}
Run Code Online (Sandbox Code Playgroud)
哪一个更好(考虑到这些变量永远不会改变)
namespace General
{
public static class Globals
{
public static double GMinimum1 = 1e-1;
public static double GMaximum1 = 1e+1;
}
}
Run Code Online (Sandbox Code Playgroud)