Why does the first line compile and the second not?
string var = "123";
var string = "123";
I mean string
and var
should be both keywords..
Keywords are the words whose meaning has already been explained to the C compiler. They have a specific meaning and they implement specific C language features. Keywords can be used only for their intended purpose. They cannot be used as names for variables or other user-defined program elements.
In a computer language, a reserved word (also known as a reserved identifier) is a word that cannot be used as an identifier, such as the name of a variable, function, or label – it is "reserved from use".
Keywords are reserved words that have a special meaning to the Java compiler. As Java compiler reserves these words for its own use so they are not available as names for variables or methods.
Reserved keywords cannot be used as variable names. Reserved keywords are ALL, AND, BY, EQ, GE, GT, LE, LT, NE, NOT, OR, TO, and WITH. Variable names can be defined with any mixture of uppercase and lowercase characters, and case is preserved for display purposes.
var
is a contextual keyword, whereas string
is not.
Contextual keywords are
used to provide a specific meaning in the code, but it is not a reserved word in C#.
This is why you can use var
as a variable name.
Presumably, this is for backwards compatibility. If var
were introduced as a proper keyword, old code that uses var
as a variable name would break.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With