Why built-in types in C# are language keywords?

有些话、适合烂在心里 提交于 2021-02-18 20:43:11

问题


In C#, identifiers such as int or string are actually language level keywords.
What is the reason for that?

Note that if the authors wanted to disallow user types with these names, that could have made that a semantic error, not syntax error.

Some clarifications based on answers:

  1. They are keywords because it makes parsing possible/easier
    I do not see why, as I am developing a parser, and having Type.Rule = Identifier is much simpler than Type.Rule = Identifier | "int" | "string" | ....

  2. They are keywords because they are special aliases
    var and dynamic are special things as well, but not keywords (for compatibility reasons, nevertheless it demonstrates that being a keyword is not necessary to be special). In a different example, applying [Serializable] to a type produces magic IL metadata modifier serializable instead of standard custom attribute. But it is still not a keyword.

  3. They are keywords because they were keywords in other languages
    Good answer, but then, why are they keywords in other languages? Also, it is certainly possible to highlight them in blue without them being keywords, so why bring that in from other languages?


回答1:


As far as I've read, the C# designers wanted to allow the typical type names as they are usual in C-style languages, namely string, int, and so on. At the same time, each of these types has to have a full-qualified type names such as System.String and System.Int32. Therefore, it was decided to provide aliases for these frequently used types.

If I find the source for this statement again, I'll add the link.

In other CLI-based languages, the same full-qualified type identifiers are valid. However, type names such as int or string might not be usual in such languages, so other aliases might be provided there.

One possible advantage of using the type alias may be improved readability, which is why there is a StyleCop rule that enforces use of the alias over the regular type name. The point about brevity is also mentioned in this thread on the same topic.




回答2:


Almost language I have learnt like this : C, C++, Java, Pascal, C#. I don't really sure, but according to class Compiler Design I have learnt at University (At this course, we will learnt how people wrote compiler, step by step, and implement an own compiler), the main reason for your question is : for easier at Lexical Analysis phrase

When you code, all of code just AN simple string. and Compiler must do many phrase before really compile it.

For example, when you type :

int a = 5;

The first phrase is Lexical Analysis must give a dictionary like below and send to Parser pharse:

    int ---> identifiers
    a   ---> Variable
    =   ---> Operator(=)
    5   ---> Integer
    ;   ---> ;

And how Lexical Analysis know this : first, It will build a table of dictionary and search for string you input. When the first tokenizer meet, it will STOP and take that tokenizer. (It's important ! )

the dictionary like this :

if   ---> if
then ---> then
int  ---> int
.... // all keywords here
[a-z][a-z0-9_]* ---> variable  // example of regular expression :  I don't sure it's true, just something like this :D

So, if the language allow you named int just like an variable. such as :

int int = 5;

The above method for lexical analysis is broken, when it reads second int, it doesn't know it's a variable or keyword, and must have more complicate steps to determine it.

I don't say it cannot, but it's more complicate and slower when compile and doesn't need to. Just say simple to programmer : "Hey, DON'T DO THAT, or, I will not compile your program :)) "

Hope this helpful :)




回答3:


This is because int and other special value types that are built into the C# language are not really types at all but aliases of the .NET Framework System types.

The reason this is a syntax error and not a semantic error is simply because the errors are detected in the syntactical error detection phase, which happens before the semantical one. The syntactical error detection has all the information it needs to determine if int is used as a type or as something else. Let's say we have the following rule:

declaration = type identifier ;

The syntactical phase checks if identifier is [a-Z]([a-Z]+ | [0-9]+)+ and is not a reserved keyword or alias, which in the case you described it is. So it makes total sense to name this a syntax error.



来源:https://stackoverflow.com/questions/11591314/why-built-in-types-in-c-sharp-are-language-keywords

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!