Idempotent modifiers in C#

C#Csc

C# Problem Overview


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 consts. So this seems to mimic C modifiers, as they are idempotent.

However, if I write:

private readonly readonly int a;

The compiler does warn me of the duplicated readonly.

So what's going on here? Are modifiers idempotent or not?


csc version 1.0.0.50618

C# Solutions


Solution 1 - C#

It's a bug in the compiler - at least in Roslyn version 1.0.0.50618. From section 8.5.2 of the C# 5 specification:

> A local-constant-declaration declares one or more local constants. > > local-constant-declaration:
>    const type constant-declarators
> > constant-declarators:
>   constant-declarator
>   constant-declarators , constant-declarator
> > constant-declarator: >   identifier = constant-expression

As you can see, that grammar doesn't allow for const const const bool flag = true;.

I've filed a bug against Roslyn so that it can get fixed.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionxdevel2000View Question on Stackoverflow
Solution 1 - C#Jon SkeetView Answer on Stackoverflow