6

I have a C# 4.0 parser. It accepts 'dynamic' as a keyword as a type. My parser trips over statements found in working C# 3.0 programs of the form of:

dynamic = <exp> ;

So, it dynamic really a keyword? Or can it still be used as an arbitrary identifier name? (If so, why isn't 'int' treated the same way)?

Is there a reference spec somewhere that states whether dynamic is keyword? The latest ECMA C# 4 specification doesn't even mention 'dynamic', and the best I can find at the MS site is a "preliminary specification" which says its a keyword but I suspect that's just sloppy writing.

AndreyAkinshin
  • 18,603
  • 29
  • 96
  • 155
Ira Baxter
  • 93,541
  • 22
  • 172
  • 341

3 Answers3

10

dynamic is a contextual keyword as of C# 4.0. The fourth edition of the ECMA C# spec does not refer to C# 4.0. (Note the 2006 publication date.)

MSDN describes it as a keyword.

It's also in this table of contextual keywords. (Scroll down.)

Sean Devlin
  • 1,662
  • 12
  • 17
8

It's not a "normal" keyword like if, for etc.

It's a contextual keyword like yield, from etc.

In particular, this compiles:

object dynamic = 0; // dynamic is a contextual keyword

but this doesn't:

object string = 0; // string is a regular keyword

You'd have to "escape" string like this:

object @string = 0;

This makes a lot of sense in terms of backward compatibility: it's unlikely that many people will have created a type called dynamic (which would cause ambiguity; IIRC the "real" type wins in this case) whereas it's very likely that existing code uses variables called dynamic.

In some ways it doesn't even need to be a contextual keyword (and I don't believe the specification ever explicitly refers to it as such) - you can think of it as the name of a type which is always available (like string and object). I would imagine that string etc were made keywords from v1 to avoid confusion, but adding genuine keywords (which can't be used as identifiers) would have a high compatibility cost now.

Ajay
  • 18,086
  • 12
  • 59
  • 105
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Great (I actually believe you because that would avoid some backward compatibility issues). But... where is the authoritative source you are using to get this specific fact? – Ira Baxter Jan 23 '10 at 17:59
  • @Ira: I'm looking at a copy of the C# 4 specification which *doesn't* list `dynamic` amongst keywords; there's no list of contextual keywords. I'm editing for a bit more information... – Jon Skeet Jan 23 '10 at 18:01
  • Great explanation Jon! But I feel using the "Reserved" word in place of "Normal" would have been more apt as there are two types of keywords defined in C# namely reserved and contextual. – RBT May 31 '16 at 05:56
  • @RBT: That's not how the spec describes it. "In some places in the grammar, specific identifiers have special meaning, but are not keywords. Such identifiers are sometimes referred to as “contextual keywords”." There's no such term as "reserved keyword". – Jon Skeet May 31 '16 at 08:39
1

It's a keyword. (see edit below) I'm a little confused on what you mean by "why isn't 'int' treated the same way?"

You can't do this: int int = 5;

But you can do this, although it's not good practice: int @int = 5;

The C# 4.0 spec does mention dynamic in section 20.1.

EDIT: more info...

Using dynamic as a variable name is allowed, so it's not a keyword (but it is a contextual keyword - see Sean Devlin's post). See the screenshot below using .NET 4.0 beta

dynamic

Check out Chris Burrow's blog post. An interesting quote from the post:

As you can see, the publicly visible members that use the type dynamic actually, behind the scenes, use the type object when they are emitted. There is no framework type "dynamic." However, those "objects" are all decorated in such a way that the compiler (or anyone else) can tell that they are meant to be handled dynamically.

Community
  • 1
  • 1
Ahmad Mageed
  • 94,561
  • 19
  • 163
  • 174
  • How precisely do you know this? ... You are asserting the the C#3.0 program that contain the assignments to 'dynamic' are invalid in C#4.0 (that breaks backwards compatibility which is usually a no-no)? If the assignment *is* valid in C3.0, then why wouldn't int=7; be allowed? – Ira Baxter Jan 23 '10 at 17:39
  • I think each major version of the language has added keywords and broken backwards compatibility. E.g. `foreach` in C# 2.0 and `var` in C# 3.0. – Sean Devlin Jan 23 '10 at 17:52
  • ... Yes, I've seen that C# 4.0 spec; thats the one I don't trust as to its characaterization of actual "keyword"ness. The C# 3.0 specification has an explicition section on keywords; the C# 4.0 spec does not and so its hard to tell if this detail is addressed accurately. (I'm sure the C# 4.0 compiler has an opinion, but so will angry C3.0 coders that used dynamic as an identifier name.) – Ira Baxter Jan 23 '10 at 17:57
  • It's not a regular keyword. You can tell that, because you can use it as an identifier. @Sean: `foreach` was in C# 1, and `var` is a contextual keyword. – Jon Skeet Jan 23 '10 at 18:03
  • @Jon Thanks, noted. I'm a relative newcomer to .NET, so my history is a little off. – Sean Devlin Jan 23 '10 at 18:07
  • 1
    @Ahmad: There's no such term as "reserved keyword" - there's just "keyword" and "contextual keyword". Downvote removed, but it would be good to see a further edit :) – Jon Skeet Jan 23 '10 at 18:12
  • @Jon: thank you. I have your 2nd ed. on order (and MEAP). Looking forward to it :) – Ahmad Mageed Jan 23 '10 at 18:23