C # "dynamic" keyword ... is it really a RESERVED keyword or just an identifier that means something special when used as a type?

I have a C # 4.0 parser. It accepts "dynamic" as a keyword as a type. My parser session statements found in C # 3.0 work programs in the form:

dynamic = <exp> ; 

So is this dynamic really a keyword? Or can it be used as the name of an arbitrary identifier? (If so, why isn’t "int" handled this way)?

Is there a reference specification somewhere where it says if the dynamic keyword is? The latest ECMA C # 4 specification doesn't even mention “dynamic,” and the best I can find on the MS website is the “preliminary specification,” which talks about the keyword, but I suspect it's just sloppy writing.

+6
c # keyword
source share
3 answers

dynamic is a contextual keyword with C # 4.0. The fourth edition of the ECMA C # specification does not apply to C # 4.0. (Note the publication date in 2006.)

MSDN describes it as a keyword.

Also in this table are contextual keywords . (Scroll down.)

+10
source share

This is not a "normal" keyword, such as if , for , etc.

This is a contextual keyword such as yield , from , etc.

In particular, this compiles:

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

but this is not so:

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

You need to "escape" the string as follows:

 object @string = 0; 

This is of great importance from the point of view of backward compatibility: it is unlikely that many people will create a type called dynamic (which will cause ambiguity; in this case, the IIRC, the "real" type wins), while it is very likely that the existing code uses variables, called dynamic .

In a sense, it should not even be a contextual keyword (and I do not believe that a specification ever explicitly refers to it as such) - you can think of it as a type name that is always available (for example, string and object ) . I would suggest that string , etc. They were made by keywords from v1 to avoid confusion, but adding genuine keywords (which cannot be used as identifiers) will now have a high compatibility cost.

+7
source share

This is a keyword. (see below). I'm a little confused by what you mean by "why isn't" int "being treated the same?"

You cannot do this: int int = 5;

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

The C # 4.0 specification mentions dynamic in section 20.1.

EDIT: more info ...

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

dynamic

Check out Chris Burrow blog post . Interesting quote from the post:

As you can see, public members that use the dynamic type actually, behind the scenes, use when they are emitted. There is no type of structure "dynamic". However, these “objects” are all designed in such a way that the compiler (or anyone else) can say that they are designed to be processed dynamically.

+1
source share

All Articles