Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using of Keywords as variable names

Tags:

c#

.net

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

like image 957
Impostor Avatar asked Mar 16 '21 11:03

Impostor


People also ask

Can C keywords be used as variable names?

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.

Can reserved keywords be used as variable names?

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

Why can we use keyword as a variable name in Java?

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.

What words Cannot be used as variable names?

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.


1 Answers

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.

like image 86
Sweeper Avatar answered Sep 20 '22 19:09

Sweeper