Impostor
Impostor

Reputation: 2050

Using of Keywords as variable names

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

Upvotes: 6

Views: 275

Answers (1)

Sweeper
Sweeper

Reputation: 270995

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.

Upvotes: 12

Related Questions