我知道这可能只是术语不匹配,但如果我没记错的话我相信 C# 是?除非我遗漏了一些明显的东西??
...
private const uint URL_COUNT = 18;
private string[] _urls;
public Redirector()
{
this._urls = new string[URL_COUNT];
...
}
...
结果为“需要一个常量值”并在数组定义中下划线 URL_COUNT??
如果 URL_COUNT 不是const -ant 值,那又是什么呢?!?!
编辑 ew,我想了想然后我要疯了。我很高兴没有人可以重复这一点,因为这意味着它只是本地的事情。 感谢你们的帮助。
最佳答案
只有当您同时提供维度长度和数组初始值设定项时,才会编译失败。例如:
this._urls = new string[URL_COUNT];
会很好,但是:
this._urls = new string[URL_COUNT] { "One", "Two" };
不会。后者需要一个常量表达式。请注意,const 变量不是常量表达式,只是一个常量值。来自 C# 规范 (3.0) par 12.6:
When an array creation expression includes both explicit dimension lengths and an array initializer, the lengths must be constant expressions and the number of elements at each nesting level must match the corresponding dimension length.
关于c# - 这怎么可能不是一个常数值呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/772263/