As it turns out, you can create a class named var
and instantiate it with new var();
. What influenced the C# developers to allow this to happen with var
but not other keywords?
Asked
Active
Viewed 126 times
0

user1306322
- 8,561
- 18
- 61
- 122
1 Answers
10
var
was only introduced in C# 3.0. It wasn't a keyword at all in older versions of the language. The designers presumably didn't anticipate creating a var
keyword at the time they first designed the language, so code was always allowed to use it as an identifier.
That's why var
is only a contextual keyword. If it were made a reserved word, existing code would break in the C# 3.0 compiler until all occurrences of var
were renamed.

BoltClock
- 700,868
- 160
- 1,392
- 1,356
-
as he said, var does not represent anything aside of implicit type – MrVoid Apr 04 '18 at 10:54
-
Was there a precedent with other keywords which disallowed previously valid type names from being used? – user1306322 Apr 04 '18 at 10:54
-
2@user1306322 - you can also have variables called e.g. `async`. Basically, any new keywords added after C# 1 tend to be contextual. Can't think of any that aren't. – Damien_The_Unbeliever Apr 04 '18 at 10:55
-
3https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/#contextual-keywords – Hans Kesting Apr 04 '18 at 10:55