54

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

DavidG
120k13 gold badges233 silver badges239 bronze badges
asked Nov 13, 2015 at 14:23
15
  • 13
    That the first line compiles is a bug in whichever compiler you're using - which you haven't told us. It doesn't compile with Roslyn... Commented Nov 13, 2015 at 14:28
  • 2
    @JonSkeet It doesn't compile if you try to put it as field. It does compile inside a method static void Main(string[] args) { const const const bool flag = true; } Commented Nov 13, 2015 at 14:30
  • 2
    @JakubLortz: Nope, still creates an error (Type expected) in VS2010. Commented Nov 13, 2015 at 14:31
  • 3
    @JakubLortz: Ah, thanks for that. This is why short but complete examples are important. Will double check that it's invalid according to the spec, but it does look like it's just a compiler bug. Commented Nov 13, 2015 at 14:40
  • 7
    @SteffenWinkler No, it applies only to C# version 6, it's nothing to do with the version of Visual Studio. I could have written that code with Notepad/Sublime and compiled it that way. Commented Nov 13, 2015 at 17:45

1 Answer 1

69

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.

answered Nov 13, 2015 at 14:43
Sign up to request clarification or add additional context in comments.

1 Comment

This pull request looks to be the fix.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.