相关疑难解决方法(0)

为什么我不能拥有"public static const string S ="stuff";在我的班级?

在尝试编译我的类时,我收到一个错误:

常量'NamespaceName.ClassName.CONST_NAME'不能标记为静态.

在线:

public static const string CONST_NAME = "blah";
Run Code Online (Sandbox Code Playgroud)

我可以用Java一直这样做.我究竟做错了什么?为什么不让我这样做?

c# const constants

297
推荐指数
5
解决办法
11万
查看次数

为什么静态只读字段不能隐式转换常量?

鉴于以下代码,我想知道为什么在编译失败时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)

c# static constants

21
推荐指数
2
解决办法
1547
查看次数

static和const变量之间的区别

在声明全局变量时,"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)

c# variables static global const

10
推荐指数
3
解决办法
4万
查看次数

标签 统计

c# ×3

const ×2

constants ×2

static ×2

global ×1

variables ×1