Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 847
Ira Baxter Avatar asked Jan 23 '10 17:01

Ira Baxter


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is C full form?

History: The name C is derived from an earlier programming language called BCPL (Basic Combined Programming Language). BCPL had another language based on it called B: the first letter in BCPL.


2 Answers

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.)

like image 110
Sean Devlin Avatar answered Sep 23 '22 07:09

Sean Devlin


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.

like image 26
Jon Skeet Avatar answered Sep 23 '22 07:09

Jon Skeet