[英]Idempotent modifiers in C#
I noticed that if I write something like: 我注意到如果我写了类似的东西:
static void Main(string[] args)
{
const const const bool flag = true;
}
The compiler doesn't warn me of the multiple const
s. 编译器不会警告我多个const
。 So this seems to mimic C modifiers, as they are idempotent. 所以这似乎模仿C修饰符,因为它们是幂等的。
However, if I write: 但是,如果我写:
private readonly readonly int a;
The compiler does warn me of the duplicated readonly
. 编译器确实警告我重复readonly
。
So what's going on here? 那么这里发生了什么? Are modifiers idempotent or not? 修饰语是否是幂等的?
csc version 1.0.0.50618 csc版本1.0.0.50618
It's a bug in the compiler - at least in Roslyn version 1.0.0.50618. 这是编译器中的一个错误 - 至少在Roslyn版本1.0.0.50618中。 From section 8.5.2 of the C# 5 specification: 从C#5规范的第8.5.2节:
A local-constant-declaration declares one or more local constants. local-constant-declaration声明一个或多个局部常量。
local-constant-declaration : 本地常量声明 :
const
type constant-declaratorsconst
类型 常量声明符constant-declarators : 常量声明者 :
constant-declarator 常数声明
constant-declarators,
constant-declarator 常量声明符,
常量声明符constant-declarator : identifier
=
constant-expression constant-declarator : identifier=
constant-expression
As you can see, that grammar doesn't allow for const const const bool flag = true;
如您所见,该语法不允许const const const bool flag = true;
. 。
I've filed a bug against Roslyn so that it can get fixed. 我已经提交了一个针对Roslyn的错误,以便它可以修复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.